Skip to content

Generic spread expressions in object literals #28234

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 8 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 22 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9840,6 +9840,10 @@ namespace ts {
return symbol ? getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol) : undefined;
}

function isNonGenericObjectType(type: Type) {
return !!(type.flags & TypeFlags.Object) && !isGenericMappedType(type);
}

/**
* Since the source of spread types are object literals, which are not binary,
* this function should be called in a left folding style, with left = previous result of getSpreadType
Expand Down Expand Up @@ -9868,6 +9872,23 @@ namespace ts {
return left;
}

if (isGenericObjectType(left) || isGenericObjectType(right)) {
if (isEmptyObjectType(left)) {
return right;
}
// When the left type is an intersection, we may need to merge the last constituent of the
// intersection with the right type. For example when the left type is 'T & { a: string }'
// and the right type is '{ b: string }' we produce 'T & { a: string, b: string }'.
if (left.flags & TypeFlags.Intersection) {
const types = (<IntersectionType>left).types;
const lastLeft = types[types.length - 1];
if (isNonGenericObjectType(lastLeft) && isNonGenericObjectType(right)) {
return getIntersectionType(concatenate(types.slice(0, types.length - 1), [getSpreadType(lastLeft, right, symbol, typeFlags, objectFlags)]));
}
}
return getIntersectionType([left, right]);
}

const members = createSymbolTable();
const skippedPrivateMembers = createUnderscoreEscapedMap<boolean>();
let stringIndexInfo: IndexInfo | undefined;
Expand Down Expand Up @@ -17697,9 +17718,8 @@ namespace ts {
}

function isValidSpreadType(type: Type): boolean {
return !!(type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.NonPrimitive) ||
return !!(type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.NonPrimitive | TypeFlags.Object | TypeFlags.InstantiableNonPrimitive) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this InstantiableNonPrimitive case, shouldn't this be checking that the constraint of type, if it exists, isValidSpreadType?

getFalsyFlags(type) & TypeFlags.DefinitelyFalsy && isValidSpreadType(removeDefinitelyFalsyTypes(type)) ||
type.flags & TypeFlags.Object && !isGenericMappedType(type) ||
type.flags & TypeFlags.UnionOrIntersection && every((<UnionOrIntersectionType>type).types, isValidSpreadType));
}

Expand Down
5 changes: 1 addition & 4 deletions tests/baselines/reference/objectRestNegative.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ tests/cases/conformance/types/rest/objectRestNegative.ts(6,10): error TS2322: Ty
tests/cases/conformance/types/rest/objectRestNegative.ts(9,31): error TS2462: A rest element must be last in a destructuring pattern.
tests/cases/conformance/types/rest/objectRestNegative.ts(11,30): error TS7008: Member 'x' implicitly has an 'any' type.
tests/cases/conformance/types/rest/objectRestNegative.ts(11,33): error TS7008: Member 'y' implicitly has an 'any' type.
tests/cases/conformance/types/rest/objectRestNegative.ts(12,17): error TS2700: Rest types may only be created from object types.
tests/cases/conformance/types/rest/objectRestNegative.ts(17,9): error TS2701: The target of an object rest assignment must be a variable or a property access.


==== tests/cases/conformance/types/rest/objectRestNegative.ts (7 errors) ====
==== tests/cases/conformance/types/rest/objectRestNegative.ts (6 errors) ====
let o = { a: 1, b: 'no' };
var { ...mustBeLast, a } = o;
~~~~~~~~~~
Expand All @@ -34,8 +33,6 @@ tests/cases/conformance/types/rest/objectRestNegative.ts(17,9): error TS2701: Th
~
!!! error TS7008: Member 'y' implicitly has an 'any' type.
let { x, ...rest } = t;
~~~~
!!! error TS2700: Rest types may only be created from object types.
return rest;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/objectRestNegative.types
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ function stillMustBeLast({ ...mustBeLast, a }: { a: number, b: string }): void {
>b : string
}
function generic<T extends { x, y }>(t: T) {
>generic : <T extends { x: any; y: any; }>(t: T) => any
>generic : <T extends { x: any; y: any; }>(t: T) => { y: any; }
>x : any
>y : any
>t : T

let { x, ...rest } = t;
>x : any
>rest : any
>rest : { y: any; }
>t : T

return rest;
>rest : any
>rest : { y: any; }
}

let rest: { b: string }
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/objectSpread.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ let a = 12;
let shortCutted: { a: number, b: string } = { ...o, a }
// non primitive
let spreadNonPrimitive = { ...<object>{}};

// generic spreads
function genericSpread<T, U>(t: T, u: U, v: T | U, w: T | { s: string }, obj: { x: number }) {
let x01 = { ...t };
let x02 = { ...t, ...t };
let x03 = { ...t, ...u };
let x04 = { ...u, ...t };
let x05 = { a: 5, b: 'hi', ...t };
let x06 = { ...t, a: 5, b: 'hi' };
let x07 = { a: 5, b: 'hi', ...t, c: true, ...obj };
let x09 = { a: 5, ...t, b: 'hi', c: true, ...obj };
let x10 = { a: 5, ...t, b: 'hi', ...u, ...obj };
let x11 = { ...v };
let x12 = { ...v, ...obj };
let x13 = { ...w };
let x14 = { ...w, ...obj };
let x15 = { ...t, ...v };
let x16 = { ...t, ...w };
let x17 = { ...t, ...w, ...obj };
let x18 = { ...t, ...v, ...w };
}


//// [objectSpread.js]
Expand Down Expand Up @@ -214,3 +235,23 @@ var a = 12;
var shortCutted = __assign({}, o, { a: a });
// non primitive
var spreadNonPrimitive = __assign({}, {});
// generic spreads
function genericSpread(t, u, v, w, obj) {
var x01 = __assign({}, t);
var x02 = __assign({}, t, t);
var x03 = __assign({}, t, u);
var x04 = __assign({}, u, t);
var x05 = __assign({ a: 5, b: 'hi' }, t);
var x06 = __assign({}, t, { a: 5, b: 'hi' });
var x07 = __assign({ a: 5, b: 'hi' }, t, { c: true }, obj);
var x09 = __assign({ a: 5 }, t, { b: 'hi', c: true }, obj);
var x10 = __assign({ a: 5 }, t, { b: 'hi' }, u, obj);
var x11 = __assign({}, v);
var x12 = __assign({}, v, obj);
var x13 = __assign({}, w);
var x14 = __assign({}, w, obj);
var x15 = __assign({}, t, v);
var x16 = __assign({}, t, w);
var x17 = __assign({}, t, w, obj);
var x18 = __assign({}, t, v, w);
}
114 changes: 114 additions & 0 deletions tests/baselines/reference/objectSpread.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,117 @@ let shortCutted: { a: number, b: string } = { ...o, a }
let spreadNonPrimitive = { ...<object>{}};
>spreadNonPrimitive : Symbol(spreadNonPrimitive, Decl(objectSpread.ts, 119, 3))

// generic spreads
function genericSpread<T, U>(t: T, u: U, v: T | U, w: T | { s: string }, obj: { x: number }) {
>genericSpread : Symbol(genericSpread, Decl(objectSpread.ts, 119, 42))
>T : Symbol(T, Decl(objectSpread.ts, 122, 23))
>U : Symbol(U, Decl(objectSpread.ts, 122, 25))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>T : Symbol(T, Decl(objectSpread.ts, 122, 23))
>u : Symbol(u, Decl(objectSpread.ts, 122, 34))
>U : Symbol(U, Decl(objectSpread.ts, 122, 25))
>v : Symbol(v, Decl(objectSpread.ts, 122, 40))
>T : Symbol(T, Decl(objectSpread.ts, 122, 23))
>U : Symbol(U, Decl(objectSpread.ts, 122, 25))
>w : Symbol(w, Decl(objectSpread.ts, 122, 50))
>T : Symbol(T, Decl(objectSpread.ts, 122, 23))
>s : Symbol(s, Decl(objectSpread.ts, 122, 59))
>obj : Symbol(obj, Decl(objectSpread.ts, 122, 72))
>x : Symbol(x, Decl(objectSpread.ts, 122, 79))

let x01 = { ...t };
>x01 : Symbol(x01, Decl(objectSpread.ts, 123, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))

let x02 = { ...t, ...t };
>x02 : Symbol(x02, Decl(objectSpread.ts, 124, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))

let x03 = { ...t, ...u };
>x03 : Symbol(x03, Decl(objectSpread.ts, 125, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>u : Symbol(u, Decl(objectSpread.ts, 122, 34))

let x04 = { ...u, ...t };
>x04 : Symbol(x04, Decl(objectSpread.ts, 126, 7))
>u : Symbol(u, Decl(objectSpread.ts, 122, 34))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))

let x05 = { a: 5, b: 'hi', ...t };
>x05 : Symbol(x05, Decl(objectSpread.ts, 127, 7))
>a : Symbol(a, Decl(objectSpread.ts, 127, 15))
>b : Symbol(b, Decl(objectSpread.ts, 127, 21))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))

let x06 = { ...t, a: 5, b: 'hi' };
>x06 : Symbol(x06, Decl(objectSpread.ts, 128, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>a : Symbol(a, Decl(objectSpread.ts, 128, 21))
>b : Symbol(b, Decl(objectSpread.ts, 128, 27))

let x07 = { a: 5, b: 'hi', ...t, c: true, ...obj };
>x07 : Symbol(x07, Decl(objectSpread.ts, 129, 7))
>a : Symbol(a, Decl(objectSpread.ts, 129, 15))
>b : Symbol(b, Decl(objectSpread.ts, 129, 21))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>c : Symbol(c, Decl(objectSpread.ts, 129, 36))
>obj : Symbol(obj, Decl(objectSpread.ts, 122, 72))

let x09 = { a: 5, ...t, b: 'hi', c: true, ...obj };
>x09 : Symbol(x09, Decl(objectSpread.ts, 130, 7))
>a : Symbol(a, Decl(objectSpread.ts, 130, 15))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>b : Symbol(b, Decl(objectSpread.ts, 130, 27))
>c : Symbol(c, Decl(objectSpread.ts, 130, 36))
>obj : Symbol(obj, Decl(objectSpread.ts, 122, 72))

let x10 = { a: 5, ...t, b: 'hi', ...u, ...obj };
>x10 : Symbol(x10, Decl(objectSpread.ts, 131, 7))
>a : Symbol(a, Decl(objectSpread.ts, 131, 15))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>b : Symbol(b, Decl(objectSpread.ts, 131, 27))
>u : Symbol(u, Decl(objectSpread.ts, 122, 34))
>obj : Symbol(obj, Decl(objectSpread.ts, 122, 72))

let x11 = { ...v };
>x11 : Symbol(x11, Decl(objectSpread.ts, 132, 7))
>v : Symbol(v, Decl(objectSpread.ts, 122, 40))

let x12 = { ...v, ...obj };
>x12 : Symbol(x12, Decl(objectSpread.ts, 133, 7))
>v : Symbol(v, Decl(objectSpread.ts, 122, 40))
>obj : Symbol(obj, Decl(objectSpread.ts, 122, 72))

let x13 = { ...w };
>x13 : Symbol(x13, Decl(objectSpread.ts, 134, 7))
>w : Symbol(w, Decl(objectSpread.ts, 122, 50))

let x14 = { ...w, ...obj };
>x14 : Symbol(x14, Decl(objectSpread.ts, 135, 7))
>w : Symbol(w, Decl(objectSpread.ts, 122, 50))
>obj : Symbol(obj, Decl(objectSpread.ts, 122, 72))

let x15 = { ...t, ...v };
>x15 : Symbol(x15, Decl(objectSpread.ts, 136, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>v : Symbol(v, Decl(objectSpread.ts, 122, 40))

let x16 = { ...t, ...w };
>x16 : Symbol(x16, Decl(objectSpread.ts, 137, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>w : Symbol(w, Decl(objectSpread.ts, 122, 50))

let x17 = { ...t, ...w, ...obj };
>x17 : Symbol(x17, Decl(objectSpread.ts, 138, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>w : Symbol(w, Decl(objectSpread.ts, 122, 50))
>obj : Symbol(obj, Decl(objectSpread.ts, 122, 72))

let x18 = { ...t, ...v, ...w };
>x18 : Symbol(x18, Decl(objectSpread.ts, 139, 7))
>t : Symbol(t, Decl(objectSpread.ts, 122, 29))
>v : Symbol(v, Decl(objectSpread.ts, 122, 40))
>w : Symbol(w, Decl(objectSpread.ts, 122, 50))
}

136 changes: 136 additions & 0 deletions tests/baselines/reference/objectSpread.types
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,139 @@ let spreadNonPrimitive = { ...<object>{}};
><object>{} : object
>{} : {}

// generic spreads
function genericSpread<T, U>(t: T, u: U, v: T | U, w: T | { s: string }, obj: { x: number }) {
>genericSpread : <T, U>(t: T, u: U, v: T | U, w: T | { s: string; }, obj: { x: number; }) => void
>t : T
>u : U
>v : T | U
>w : T | { s: string; }
>s : string
>obj : { x: number; }
>x : number

let x01 = { ...t };
>x01 : T
>{ ...t } : T
>t : T

let x02 = { ...t, ...t };
>x02 : T
>{ ...t, ...t } : T
>t : T
>t : T

let x03 = { ...t, ...u };
>x03 : T & U
>{ ...t, ...u } : T & U
>t : T
>u : U

let x04 = { ...u, ...t };
>x04 : U & T
>{ ...u, ...t } : U & T
>u : U
>t : T

let x05 = { a: 5, b: 'hi', ...t };
>x05 : { a: number; b: string; } & T
>{ a: 5, b: 'hi', ...t } : { a: number; b: string; } & T
>a : number
>5 : 5
>b : string
>'hi' : "hi"
>t : T

let x06 = { ...t, a: 5, b: 'hi' };
>x06 : T & { a: number; b: string; }
>{ ...t, a: 5, b: 'hi' } : T & { a: number; b: string; }
>t : T
>a : number
>5 : 5
>b : string
>'hi' : "hi"

let x07 = { a: 5, b: 'hi', ...t, c: true, ...obj };
>x07 : { a: number; b: string; } & T & { x: number; c: boolean; }
>{ a: 5, b: 'hi', ...t, c: true, ...obj } : { a: number; b: string; } & T & { x: number; c: boolean; }
>a : number
>5 : 5
>b : string
>'hi' : "hi"
>t : T
>c : boolean
>true : true
>obj : { x: number; }

let x09 = { a: 5, ...t, b: 'hi', c: true, ...obj };
>x09 : { a: number; } & T & { x: number; b: string; c: boolean; }
>{ a: 5, ...t, b: 'hi', c: true, ...obj } : { a: number; } & T & { x: number; b: string; c: boolean; }
>a : number
>5 : 5
>t : T
>b : string
>'hi' : "hi"
>c : boolean
>true : true
>obj : { x: number; }

let x10 = { a: 5, ...t, b: 'hi', ...u, ...obj };
>x10 : { a: number; } & T & { b: string; } & U & { x: number; }
>{ a: 5, ...t, b: 'hi', ...u, ...obj } : { a: number; } & T & { b: string; } & U & { x: number; }
>a : number
>5 : 5
>t : T
>b : string
>'hi' : "hi"
>u : U
>obj : { x: number; }

let x11 = { ...v };
>x11 : T | U
>{ ...v } : T | U
>v : T | U

let x12 = { ...v, ...obj };
>x12 : (T & { x: number; }) | (U & { x: number; })
>{ ...v, ...obj } : (T & { x: number; }) | (U & { x: number; })
>v : T | U
>obj : { x: number; }

let x13 = { ...w };
>x13 : T | { s: string; }
>{ ...w } : T | { s: string; }
>w : T | { s: string; }

let x14 = { ...w, ...obj };
>x14 : (T & { x: number; }) | { x: number; s: string; }
>{ ...w, ...obj } : (T & { x: number; }) | { x: number; s: string; }
>w : T | { s: string; }
>obj : { x: number; }

let x15 = { ...t, ...v };
>x15 : T | (T & U)
>{ ...t, ...v } : T | (T & U)
>t : T
>v : T | U

let x16 = { ...t, ...w };
>x16 : T | (T & { s: string; })
>{ ...t, ...w } : T | (T & { s: string; })
>t : T
>w : T | { s: string; }

let x17 = { ...t, ...w, ...obj };
>x17 : (T & { x: number; }) | (T & { x: number; s: string; })
>{ ...t, ...w, ...obj } : (T & { x: number; }) | (T & { x: number; s: string; })
>t : T
>w : T | { s: string; }
>obj : { x: number; }

let x18 = { ...t, ...v, ...w };
>x18 : T | (T & U) | (T & { s: string; }) | (T & U & { s: string; })
>{ ...t, ...v, ...w } : T | (T & U) | (T & { s: string; }) | (T & U & { s: string; })
>t : T
>v : T | U
>w : T | { s: string; }
}

Loading