Skip to content

Properly instantiate object types created from rest parameters #34826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6609,7 +6609,9 @@ namespace ts {
}
const stringIndexInfo = getIndexInfoOfType(source, IndexKind.String);
const numberIndexInfo = getIndexInfoOfType(source, IndexKind.Number);
return createAnonymousType(symbol, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
const result = createAnonymousType(symbol, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
result.objectFlags |= ObjectFlags.ObjectRestType;
return result;
}

// Determine the control flow type associated with a destructuring declaration or assignment. The following
Expand Down Expand Up @@ -17155,7 +17157,7 @@ namespace ts {
return !!(type.flags & TypeFlags.Instantiable ||
objectFlags & ObjectFlags.Reference && ((<TypeReference>type).node || forEach(getTypeArguments(<TypeReference>type), couldContainTypeVariables)) ||
objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && type.symbol.declarations ||
objectFlags & ObjectFlags.Mapped ||
objectFlags & (ObjectFlags.Mapped | ObjectFlags.ObjectRestType) ||
type.flags & TypeFlags.UnionOrIntersection && !(type.flags & TypeFlags.EnumLiteral) && couldUnionOrIntersectionContainTypeVariables(<UnionOrIntersectionType>type));
}

Expand Down
9 changes: 5 additions & 4 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4352,14 +4352,15 @@ namespace ts {
JSLiteral = 1 << 14, // Object type declared in JS - disables errors on read/write of nonexisting members
FreshLiteral = 1 << 15, // Fresh object literal
ArrayLiteral = 1 << 16, // Originates in an array literal
ObjectRestType = 1 << 17, // Originates in object rest declaration
/* @internal */
PrimitiveUnion = 1 << 17, // Union of only primitive types
PrimitiveUnion = 1 << 18, // Union of only primitive types
/* @internal */
ContainsWideningType = 1 << 18, // Type is or contains undefined or null widening type
ContainsWideningType = 1 << 19, // Type is or contains undefined or null widening type
/* @internal */
ContainsObjectOrArrayLiteral = 1 << 19, // Type is or contains object literal type
ContainsObjectOrArrayLiteral = 1 << 20, // Type is or contains object literal type
/* @internal */
NonInferrableType = 1 << 20, // Type is or contains anyFunctionType or silentNeverType
NonInferrableType = 1 << 21, // Type is or contains anyFunctionType or silentNeverType
ClassOrInterface = Class | Interface,
/* @internal */
RequiresWidening = ContainsWideningType | ContainsObjectOrArrayLiteral,
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,7 @@ declare namespace ts {
JSLiteral = 16384,
FreshLiteral = 32768,
ArrayLiteral = 65536,
ObjectRestType = 131072,
ClassOrInterface = 3,
}
export interface ObjectType extends Type {
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,7 @@ declare namespace ts {
JSLiteral = 16384,
FreshLiteral = 32768,
ArrayLiteral = 65536,
ObjectRestType = 131072,
ClassOrInterface = 3,
}
export interface ObjectType extends Type {
Expand Down
34 changes: 34 additions & 0 deletions tests/baselines/reference/restParameterTypeInstantiation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [restParameterTypeInstantiation.ts]
// Repro from #33823

interface TestGeneric<TG> {
f: string
g: TG
}

const removeF = <TX>({ f, ...rest }: TestGeneric<TX>) => {
return rest
}

const result: number = removeF<number>({ f: '', g: 3 }).g


//// [restParameterTypeInstantiation.js]
"use strict";
// Repro from #33823
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var removeF = function (_a) {
var f = _a.f, rest = __rest(_a, ["f"]);
return rest;
};
var result = removeF({ f: '', g: 3 }).g;
35 changes: 35 additions & 0 deletions tests/baselines/reference/restParameterTypeInstantiation.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/restParameterTypeInstantiation.ts ===
// Repro from #33823

interface TestGeneric<TG> {
>TestGeneric : Symbol(TestGeneric, Decl(restParameterTypeInstantiation.ts, 0, 0))
>TG : Symbol(TG, Decl(restParameterTypeInstantiation.ts, 2, 22))

f: string
>f : Symbol(TestGeneric.f, Decl(restParameterTypeInstantiation.ts, 2, 27))

g: TG
>g : Symbol(TestGeneric.g, Decl(restParameterTypeInstantiation.ts, 3, 11))
>TG : Symbol(TG, Decl(restParameterTypeInstantiation.ts, 2, 22))
}

const removeF = <TX>({ f, ...rest }: TestGeneric<TX>) => {
>removeF : Symbol(removeF, Decl(restParameterTypeInstantiation.ts, 7, 5))
>TX : Symbol(TX, Decl(restParameterTypeInstantiation.ts, 7, 17))
>f : Symbol(f, Decl(restParameterTypeInstantiation.ts, 7, 22))
>rest : Symbol(rest, Decl(restParameterTypeInstantiation.ts, 7, 25))
>TestGeneric : Symbol(TestGeneric, Decl(restParameterTypeInstantiation.ts, 0, 0))
>TX : Symbol(TX, Decl(restParameterTypeInstantiation.ts, 7, 17))

return rest
>rest : Symbol(rest, Decl(restParameterTypeInstantiation.ts, 7, 25))
}

const result: number = removeF<number>({ f: '', g: 3 }).g
>result : Symbol(result, Decl(restParameterTypeInstantiation.ts, 11, 5))
>removeF<number>({ f: '', g: 3 }).g : Symbol(TestGeneric.g, Decl(restParameterTypeInstantiation.ts, 3, 11))
>removeF : Symbol(removeF, Decl(restParameterTypeInstantiation.ts, 7, 5))
>f : Symbol(f, Decl(restParameterTypeInstantiation.ts, 11, 40))
>g : Symbol(g, Decl(restParameterTypeInstantiation.ts, 11, 47))
>g : Symbol(TestGeneric.g, Decl(restParameterTypeInstantiation.ts, 3, 11))

33 changes: 33 additions & 0 deletions tests/baselines/reference/restParameterTypeInstantiation.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/restParameterTypeInstantiation.ts ===
// Repro from #33823

interface TestGeneric<TG> {
f: string
>f : string

g: TG
>g : TG
}

const removeF = <TX>({ f, ...rest }: TestGeneric<TX>) => {
>removeF : <TX>({ f, ...rest }: TestGeneric<TX>) => { g: TX; }
><TX>({ f, ...rest }: TestGeneric<TX>) => { return rest} : <TX>({ f, ...rest }: TestGeneric<TX>) => { g: TX; }
>f : string
>rest : { g: TX; }

return rest
>rest : { g: TX; }
}

const result: number = removeF<number>({ f: '', g: 3 }).g
>result : number
>removeF<number>({ f: '', g: 3 }).g : number
>removeF<number>({ f: '', g: 3 }) : { g: number; }
>removeF : <TX>({ f, ...rest }: TestGeneric<TX>) => { g: TX; }
>{ f: '', g: 3 } : { f: string; g: number; }
>f : string
>'' : ""
>g : number
>3 : 3
>g : number

14 changes: 14 additions & 0 deletions tests/cases/compiler/restParameterTypeInstantiation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @strict: true

// Repro from #33823

interface TestGeneric<TG> {
f: string
g: TG
}

const removeF = <TX>({ f, ...rest }: TestGeneric<TX>) => {
return rest
}

const result: number = removeF<number>({ f: '', g: 3 }).g