Skip to content

Spread & rest over intersection and unions #12552

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 12 commits into from
Nov 30, 2016
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
38 changes: 33 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3054,7 +3054,15 @@ namespace ts {
}

function getRestType(source: Type, properties: PropertyName[], symbol: Symbol): Type {
Debug.assert(!!(source.flags & TypeFlags.Object), "Rest types only support object types right now.");
source = filterType(source, t => !(t.flags & TypeFlags.Nullable));
if (source.flags & TypeFlags.Never) {
return emptyObjectType;
}

if (source.flags & TypeFlags.Union) {
return mapType(source, t => getRestType(t, properties, symbol));
}

const members = createMap<Symbol>();
const names = createMap<true>();
for (const name of properties) {
Expand Down Expand Up @@ -3095,7 +3103,7 @@ namespace ts {
let type: Type;
if (pattern.kind === SyntaxKind.ObjectBindingPattern) {
if (declaration.dotDotDotToken) {
if (!(parentType.flags & TypeFlags.Object)) {
if (!isValidSpreadType(parentType)) {
error(declaration, Diagnostics.Rest_types_may_only_be_created_from_object_types);
return unknownType;
}
Expand Down Expand Up @@ -6102,11 +6110,25 @@ namespace ts {
* this function should be called in a left folding style, with left = previous result of getSpreadType
* and right = the new element to be spread.
*/
function getSpreadType(left: Type, right: Type, isFromObjectLiteral: boolean): ResolvedType | IntrinsicType {
Debug.assert(!!(left.flags & (TypeFlags.Object | TypeFlags.Any)) && !!(right.flags & (TypeFlags.Object | TypeFlags.Any)), "Only object types may be spread.");
function getSpreadType(left: Type, right: Type, isFromObjectLiteral: boolean): Type {
if (left.flags & TypeFlags.Any || right.flags & TypeFlags.Any) {
return anyType;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: these two parts of code for left and right looks quite similar, can they be extracted to a function?

left = filterType(left, t => !(t.flags & TypeFlags.Nullable));
Copy link
Contributor

Choose a reason for hiding this comment

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

this pattern filter out nullable types is repeated at least 3 times

if (left.flags & TypeFlags.Never) {
return right;
}
Copy link
Member

Choose a reason for hiding this comment

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

You can replace all of these new lines with just this:

            left = filterType(left, t => !(t.flags & TypeFlags.Nullable));
            if (left.flags & TypeFlags.Never) {
                return right;
            }
            right = filterType(right, t => !(t.flags & TypeFlags.Nullable));
            if (right.flags & TypeFlags.Never) {
                return left;
            }
            if (left.flags & TypeFlags.Union) {
                return mapType(left, t => getSpreadType(t, right, isFromObjectLiteral));
            }
            if (right.flags & TypeFlags.Union) {
                return mapType(right, t => getSpreadType(left, t, isFromObjectLiteral));
            }

right = filterType(right, t => !(t.flags & TypeFlags.Nullable));
if (right.flags & TypeFlags.Never) {
return left;
}
if (left.flags & TypeFlags.Union) {
return mapType(left, t => getSpreadType(t, right, isFromObjectLiteral));
}
if (right.flags & TypeFlags.Union) {
return mapType(right, t => getSpreadType(left, t, isFromObjectLiteral));
}

const members = createMap<Symbol>();
const skippedPrivateMembers = createMap<boolean>();
let stringIndexInfo: IndexInfo;
Expand Down Expand Up @@ -11438,7 +11460,7 @@ namespace ts {
typeFlags = 0;
}
const type = checkExpression((memberDecl as SpreadAssignment).expression);
if (!(type.flags & (TypeFlags.Object | TypeFlags.Any))) {
if (!isValidSpreadType(type)) {
error(memberDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
return unknownType;
}
Expand Down Expand Up @@ -11516,6 +11538,12 @@ namespace ts {
}
}

function isValidSpreadType(type: Type): boolean {
return !!(type.flags & (TypeFlags.Any | TypeFlags.Null | TypeFlags.Undefined) ||
type.flags & TypeFlags.Object && !isGenericMappedType(type) ||

Choose a reason for hiding this comment

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

IIUC this should also fix #12460

type.flags & TypeFlags.UnionOrIntersection && !forEach((<UnionOrIntersectionType>type).types, t => !isValidSpreadType(t)));
}

function checkJsxSelfClosingElement(node: JsxSelfClosingElement) {
checkJsxOpeningLikeElement(node);
return jsxElementType || anyType;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/destructuring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ namespace ts {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (typeof Object.getOwnPropertySymbols === "function")
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)
t[p[i]] = s[p[i]];
return t;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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 (typeof Object.getOwnPropertySymbols === "function")
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)
t[p[i]] = s[p[i]];
return t;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectRestAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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 (typeof Object.getOwnPropertySymbols === "function")
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)
t[p[i]] = s[p[i]];
return t;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectRestForOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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 (typeof Object.getOwnPropertySymbols === "function")
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)
t[p[i]] = s[p[i]];
return t;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectRestNegative.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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 (typeof Object.getOwnPropertySymbols === "function")
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)
t[p[i]] = s[p[i]];
return t;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectRestParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 (typeof Object.getOwnPropertySymbols === "function")
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)
t[p[i]] = s[p[i]];
return t;
Expand Down
30 changes: 11 additions & 19 deletions tests/baselines/reference/objectSpreadNegative.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ tests/cases/conformance/types/spread/objectSpreadNegative.ts(25,1): error TS2322
Property 's' is missing in type '{ b: boolean; }'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(28,36): error TS2300: Duplicate identifier 'b'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(28,53): error TS2300: Duplicate identifier 'b'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(32,20): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(33,24): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(34,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(35,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(37,20): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(39,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(44,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{}' has no compatible call signatures.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(48,12): error TS2339: Property 'b' does not exist on type '{}'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(54,9): error TS2339: Property 'm' does not exist on type '{ p: number; }'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(58,14): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(61,14): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(32,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(33,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(35,20): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(37,19): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(42,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '{}' has no compatible call signatures.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(46,12): error TS2339: Property 'b' does not exist on type '{}'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(52,9): error TS2339: Property 'm' does not exist on type '{ p: number; }'.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(56,14): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/spread/objectSpreadNegative.ts(59,14): error TS2698: Spread types may only be created from object types.


==== tests/cases/conformance/types/spread/objectSpreadNegative.ts (17 errors) ====
==== tests/cases/conformance/types/spread/objectSpreadNegative.ts (15 errors) ====
let o = { a: 1, b: 'no' }

/// private propagates
Expand Down Expand Up @@ -66,13 +64,7 @@ tests/cases/conformance/types/spread/objectSpreadNegative.ts(61,14): error TS269
!!! error TS2300: Duplicate identifier 'b'.
let duplicatedSpread = { ...o, ...o }

// null, undefined and primitives are not allowed
let spreadNull = { ...null };
~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
let spreadUndefind = { ...undefined };
~~~~~~~~~~~~
!!! error TS2698: Spread types may only be created from object types.
// primitives are not allowed
let spreadNum = { ...12 };
~~~~~
!!! error TS2698: Spread types may only be created from object types.
Expand Down
8 changes: 2 additions & 6 deletions tests/baselines/reference/objectSpreadNegative.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ spread = b; // error, missing 's'
let duplicated = { b: 'bad', ...o, b: 'bad', ...o2, b: 'bad' }
let duplicatedSpread = { ...o, ...o }

// null, undefined and primitives are not allowed
let spreadNull = { ...null };
let spreadUndefind = { ...undefined };
// primitives are not allowed
let spreadNum = { ...12 };
let spreadSum = { ...1 + 1 };
spreadSum.toFixed(); // error, no methods from number
Expand Down Expand Up @@ -108,9 +106,7 @@ spread = b; // error, missing 's'
// literal repeats are not allowed, but spread repeats are fine
var duplicated = __assign({ b: 'bad' }, o, { b: 'bad' }, o2, { b: 'bad' });
var duplicatedSpread = __assign({}, o, o);
// null, undefined and primitives are not allowed
var spreadNull = __assign({}, null);
var spreadUndefind = __assign({}, undefined);
// primitives are not allowed
var spreadNum = __assign({}, 12);
var spreadSum = __assign({}, 1 + 1);
spreadSum.toFixed(); // error, no methods from number
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/restIntersection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [restIntersection.ts]
var intersection: { x: number, y: number } & { w: string, z: string };

var rest1: { y: number, w: string, z: string };
var {x, ...rest1 } = intersection;


//// [restIntersection.js]
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)
t[p[i]] = s[p[i]];
return t;
};
var intersection;
var rest1;
var x = intersection.x, rest1 = __rest(intersection, ["x"]);
19 changes: 19 additions & 0 deletions tests/baselines/reference/restIntersection.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/restIntersection.ts ===
var intersection: { x: number, y: number } & { w: string, z: string };
>intersection : Symbol(intersection, Decl(restIntersection.ts, 0, 3))
>x : Symbol(x, Decl(restIntersection.ts, 0, 19))
>y : Symbol(y, Decl(restIntersection.ts, 0, 30))
>w : Symbol(w, Decl(restIntersection.ts, 0, 46))
>z : Symbol(z, Decl(restIntersection.ts, 0, 57))

var rest1: { y: number, w: string, z: string };
>rest1 : Symbol(rest1, Decl(restIntersection.ts, 2, 3), Decl(restIntersection.ts, 3, 7))
>y : Symbol(y, Decl(restIntersection.ts, 2, 12))
>w : Symbol(w, Decl(restIntersection.ts, 2, 23))
>z : Symbol(z, Decl(restIntersection.ts, 2, 34))

var {x, ...rest1 } = intersection;
>x : Symbol(x, Decl(restIntersection.ts, 3, 5))
>rest1 : Symbol(rest1, Decl(restIntersection.ts, 2, 3), Decl(restIntersection.ts, 3, 7))
>intersection : Symbol(intersection, Decl(restIntersection.ts, 0, 3))

19 changes: 19 additions & 0 deletions tests/baselines/reference/restIntersection.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/restIntersection.ts ===
var intersection: { x: number, y: number } & { w: string, z: string };
>intersection : { x: number; y: number; } & { w: string; z: string; }
>x : number
>y : number
>w : string
>z : string

var rest1: { y: number, w: string, z: string };
>rest1 : { y: number; w: string; z: string; }
>y : number
>w : string
>z : string

var {x, ...rest1 } = intersection;
>x : number
>rest1 : { y: number; w: string; z: string; }
>intersection : { x: number; y: number; } & { w: string; z: string; }

104 changes: 104 additions & 0 deletions tests/baselines/reference/restInvalidArgumentType.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
tests/cases/compiler/restInvalidArgumentType.ts(31,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(33,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(35,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(36,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(38,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(41,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(42,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(44,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(45,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(47,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(48,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(55,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(56,13): error TS2700: Rest types may only be created from object types.
tests/cases/compiler/restInvalidArgumentType.ts(58,13): error TS2700: Rest types may only be created from object types.


==== tests/cases/compiler/restInvalidArgumentType.ts (14 errors) ====
enum E { v1, v2 };

function f<T extends { b: string }>(p1: T, p2: T[]) {
var t: T;

var i: T["b"];
var k: keyof T;

var mapped_generic: {[P in keyof T]: T[P]};
var mapped: {[P in "b"]: T[P]};

var union_generic: T | { a: number };
var union_primitive: { a: number } | number;

var intersection_generic: T & { a: number };
var intersection_premitive: { a: number } | string;

var num: number;
var str: number;

var u: undefined;
var n: null;

var a: any;

var literal_string: "string";
var literal_number: 42;

var e: E;

var {...r1} = p1; // Error, generic type paramterre
~~
!!! error TS2700: Rest types may only be created from object types.
var {...r2} = p2; // OK
var {...r3} = t; // Error, generic type paramter
~~
!!! error TS2700: Rest types may only be created from object types.

var {...r4} = i; // Error, index access
~~
!!! error TS2700: Rest types may only be created from object types.
var {...r5} = k; // Error, index
~~
!!! error TS2700: Rest types may only be created from object types.

var {...r6} = mapped_generic; // Error, generic mapped object type
~~
!!! error TS2700: Rest types may only be created from object types.
var {...r7} = mapped; // OK, non-generic mapped type

var {...r8} = union_generic; // Error, union with generic type parameter
~~
!!! error TS2700: Rest types may only be created from object types.
var {...r9} = union_primitive; // Error, union with generic type parameter
~~
!!! error TS2700: Rest types may only be created from object types.

var {...r10} = intersection_generic; // Error, intersection with generic type parameter
~~~
!!! error TS2700: Rest types may only be created from object types.
var {...r11} = intersection_premitive; // Error, intersection with generic type parameter
~~~
!!! error TS2700: Rest types may only be created from object types.

var {...r12} = num; // Error
~~~
!!! error TS2700: Rest types may only be created from object types.
var {...r13} = str; // Error
~~~
!!! error TS2700: Rest types may only be created from object types.

var {...r14} = u; // OK
var {...r15} = n; // OK

var {...r16} = a; // OK

var {...r17} = literal_string; // Error
~~~
!!! error TS2700: Rest types may only be created from object types.
var {...r18} = literal_number; // Error
~~~
!!! error TS2700: Rest types may only be created from object types.

var {...r19} = e; // Error, enum
~~~
!!! error TS2700: Rest types may only be created from object types.
}
Loading