Skip to content
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

Infer array rest as tuple if possible #26070

Merged
merged 4 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 17 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4555,11 +4555,11 @@ namespace ts {
const elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false, /*allowAsyncIterables*/ false);
const index = pattern.elements.indexOf(declaration);
if (declaration.dotDotDotToken) {
// If the parent is a tuple type, the rest element has an array type with a union of the
// If the parent is a tuple type, the rest element has a tuple type of the
// remaining tuple element types. Otherwise, the rest element has an array type with same
// element type as the parent type.
type = isTupleType(parentType) ?
getArrayLiteralType((parentType.typeArguments || emptyArray).slice(index, getTypeReferenceArity(parentType))) :
sliceTupleType(parentType, index) :
createArrayType(elementType);
}
else {
Expand Down Expand Up @@ -8526,6 +8526,20 @@ namespace ts {
return links.resolvedType;
}

function sliceTupleType(type: TupleTypeReference, index: number) {
const tuple = type.target;
if (tuple.hasRestElement) {
// don't slice off rest element
index = Math.min(index, getTypeReferenceArity(type) - 1);
}
return createTupleType(
(type.typeArguments || emptyArray).slice(index),
Math.max(0, tuple.minLength - index),
tuple.hasRestElement,
tuple.associatedNames && tuple.associatedNames.slice(index),
);
}

function getTypeFromOptionalTypeNode(node: OptionalTypeNode): Type {
const type = getTypeFromTypeNode(node.type);
return strictNullChecks ? getOptionalType(type) : type;
Expand Down Expand Up @@ -21052,7 +21066,7 @@ namespace ts {
else {
checkGrammarForDisallowedTrailingComma(node.elements, Diagnostics.A_rest_parameter_or_binding_pattern_may_not_have_a_trailing_comma);
const type = isTupleType(sourceType) ?
getArrayLiteralType((sourceType.typeArguments || emptyArray).slice(elementIndex, getTypeReferenceArity(sourceType))) :
sliceTupleType(sourceType, elementIndex) :
createArrayType(elementType);
return checkDestructuringAssignment(restExpression, type, checkMode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function bar([x, z, ...w]) { }
function foo([x, ...y] = [1, "string", true]) { }
>foo : ([x, ...y]?: [number, string, boolean]) => void
>x : number
>y : (string | boolean)[]
>y : [string, boolean]
>[1, "string", true] : [number, string, boolean]
>1 : 1
>"string" : "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ var _f = [1, "hello", true], x19 = _f[0], y19 = _f[1], z19 = _f[2], a13 = _f.sli

//// [declarationEmitDestructuringArrayPattern4.d.ts]
declare var a5: number[];
declare var x14: number, a6: number[];
declare var x15: number, y15: number, a7: number[];
declare var x16: number, y16: number, z16: number, a8: any[];
declare var x14: number, a6: [number, number];
declare var x15: number, y15: number, a7: [number];
declare var x16: number, y16: number, z16: number, a8: [];
declare var a9: (string | number | boolean)[];
declare var x17: number, a10: (string | boolean)[];
declare var x18: number, y18: string, a12: boolean[];
declare var x19: number, y19: string, z19: boolean, a13: any[];
declare var x17: number, a10: [string, boolean];
declare var x18: number, y18: string, a12: [boolean];
declare var x19: number, y19: string, z19: boolean, a13: [];
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var [...a5] = [1, 2, 3];

var [x14, ...a6] = [1, 2, 3];
>x14 : number
>a6 : number[]
>a6 : [number, number]
>[1, 2, 3] : [number, number, number]
>1 : 1
>2 : 2
Expand All @@ -17,7 +17,7 @@ var [x14, ...a6] = [1, 2, 3];
var [x15, y15, ...a7] = [1, 2, 3];
>x15 : number
>y15 : number
>a7 : number[]
>a7 : [number]
>[1, 2, 3] : [number, number, number]
>1 : 1
>2 : 2
Expand All @@ -27,7 +27,7 @@ var [x16, y16, z16, ...a8] = [1, 2, 3];
>x16 : number
>y16 : number
>z16 : number
>a8 : any[]
>a8 : []
>[1, 2, 3] : [number, number, number]
>1 : 1
>2 : 2
Expand All @@ -42,7 +42,7 @@ var [...a9] = [1, "hello", true];

var [x17, ...a10] = [1, "hello", true];
>x17 : number
>a10 : (string | boolean)[]
>a10 : [string, boolean]
>[1, "hello", true] : [number, string, boolean]
>1 : 1
>"hello" : "hello"
Expand All @@ -51,7 +51,7 @@ var [x17, ...a10] = [1, "hello", true];
var [x18, y18, ...a12] = [1, "hello", true];
>x18 : number
>y18 : string
>a12 : boolean[]
>a12 : [boolean]
>[1, "hello", true] : [number, string, boolean]
>1 : 1
>"hello" : "hello"
Expand All @@ -61,7 +61,7 @@ var [x19, y19, z19, ...a13] = [1, "hello", true];
>x19 : number
>y19 : string
>z19 : boolean
>a13 : any[]
>a13 : []
>[1, "hello", true] : [number, string, boolean]
>1 : 1
>"hello" : "hello"
Expand Down
20 changes: 19 additions & 1 deletion tests/baselines/reference/declarationsAndAssignments.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(106,5):
Property 'x' is missing in type '{ y: false; }'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,6): error TS2322: Type 'string' is not assignable to type 'number'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9): error TS2322: Type 'number' is not assignable to type 'string'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(158,16): error TS2403: Subsequent variable declarations must have the same type. Variable 'a' must be of type 'number[]', but here has type '[number, number]'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(159,19): error TS2403: Subsequent variable declarations must have the same type. Variable 'a' must be of type 'number[]', but here has type '[number]'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(160,22): error TS2403: Subsequent variable declarations must have the same type. Variable 'a3' must be of type 'any[]', but here has type '[]'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(176,16): error TS2403: Subsequent variable declarations must have the same type. Variable 'a1' must be of type '(string | boolean)[]', but here has type '[string, boolean]'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(177,19): error TS2403: Subsequent variable declarations must have the same type. Variable 'a2' must be of type 'boolean[]', but here has type '[boolean]'.
tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(178,22): error TS2403: Subsequent variable declarations must have the same type. Variable 'a3' must be of type 'any[]', but here has type '[]'.


==== tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts (22 errors) ====
==== tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts (28 errors) ====
function f0() {
var [] = [1, "hello"];
var [x] = [1, "hello"];
Expand Down Expand Up @@ -231,8 +237,14 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
var a3: any[];
var [...a] = [1, 2, 3];
var [x, ...a] = [1, 2, 3];
~
Copy link
Member

Choose a reason for hiding this comment

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

I'd change the variable names at these new errors in this test so that you're comparing with the expected type and there's no error.

!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'a' must be of type 'number[]', but here has type '[number, number]'.
var [x, y, ...a] = [1, 2, 3];
~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'a' must be of type 'number[]', but here has type '[number]'.
var [x, y, z, ...a3] = [1, 2, 3];
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'a3' must be of type 'any[]', but here has type '[]'.
[...a] = [1, 2, 3];
[x, ...a] = [1, 2, 3];
[x, y, ...a] = [1, 2, 3];
Expand All @@ -249,8 +261,14 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
var a3: any[];
var [...a0] = [1, "hello", true];
var [x, ...a1] = [1, "hello", true];
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'a1' must be of type '(string | boolean)[]', but here has type '[string, boolean]'.
var [x, y, ...a2] = [1, "hello", true];
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'a2' must be of type 'boolean[]', but here has type '[boolean]'.
var [x, y, z, ...a3] = [1, "hello", true];
~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'a3' must be of type 'any[]', but here has type '[]'.
[...a0] = [1, "hello", true];
[x, ...a1] = [1, "hello", true];
[x, y, ...a2] = [1, "hello", true];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var [,,,...c10] = [1, 2, 3, 4, "hello"];
> : undefined
> : undefined
> : undefined
>c10 : (string | number)[]
>c10 : [number, string]
>[1, 2, 3, 4, "hello"] : [number, number, number, number, string]
>1 : 1
>2 : 2
Expand All @@ -159,7 +159,7 @@ var [,,,...c10] = [1, 2, 3, 4, "hello"];
var [c11, c12, ...c13] = [1, 2, "string"];
>c11 : number
>c12 : number
>c13 : string[]
>c13 : [string]
>[1, 2, "string"] : [number, number, string]
>1 : 1
>2 : 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var [,,,...c10] = [1, 2, 3, 4, "hello"];
> : undefined
> : undefined
> : undefined
>c10 : (string | number)[]
>c10 : [number, string]
>[1, 2, 3, 4, "hello"] : [number, number, number, number, string]
>1 : 1
>2 : 2
Expand All @@ -159,7 +159,7 @@ var [,,,...c10] = [1, 2, 3, 4, "hello"];
var [c11, c12, ...c13] = [1, 2, "string"];
>c11 : number
>c12 : number
>c13 : string[]
>c13 : [string]
>[1, 2, "string"] : [number, number, string]
>1 : 1
>2 : 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ var [,,,...c10] = [1, 2, 3, 4, "hello"];
> : undefined
> : undefined
> : undefined
>c10 : (string | number)[]
>c10 : [number, string]
>[1, 2, 3, 4, "hello"] : [number, number, number, number, string]
>1 : 1
>2 : 2
Expand All @@ -159,7 +159,7 @@ var [,,,...c10] = [1, 2, 3, 4, "hello"];
var [c11, c12, ...c13] = [1, 2, "string"];
>c11 : number
>c12 : number
>c13 : string[]
>c13 : [string]
>[1, 2, "string"] : [number, number, string]
>1 : 1
>2 : 2
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/destructuringTuple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [destructuringTuple.ts]
declare var tuple: [boolean, number, ...string[]];

const [a, b, c, ...rest] = tuple;

declare var receiver: typeof tuple;

[...receiver] = tuple;


//// [destructuringTuple.js]
"use strict";
var a = tuple[0], b = tuple[1], c = tuple[2], rest = tuple.slice(3);
receiver = tuple.slice(0);
19 changes: 19 additions & 0 deletions tests/baselines/reference/destructuringTuple.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/destructuringTuple.ts ===
declare var tuple: [boolean, number, ...string[]];
>tuple : Symbol(tuple, Decl(destructuringTuple.ts, 0, 11))

const [a, b, c, ...rest] = tuple;
>a : Symbol(a, Decl(destructuringTuple.ts, 2, 7))
>b : Symbol(b, Decl(destructuringTuple.ts, 2, 9))
>c : Symbol(c, Decl(destructuringTuple.ts, 2, 12))
>rest : Symbol(rest, Decl(destructuringTuple.ts, 2, 15))
>tuple : Symbol(tuple, Decl(destructuringTuple.ts, 0, 11))

declare var receiver: typeof tuple;
>receiver : Symbol(receiver, Decl(destructuringTuple.ts, 4, 11))
>tuple : Symbol(tuple, Decl(destructuringTuple.ts, 0, 11))

[...receiver] = tuple;
>receiver : Symbol(receiver, Decl(destructuringTuple.ts, 4, 11))
>tuple : Symbol(tuple, Decl(destructuringTuple.ts, 0, 11))

22 changes: 22 additions & 0 deletions tests/baselines/reference/destructuringTuple.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/destructuringTuple.ts ===
declare var tuple: [boolean, number, ...string[]];
>tuple : [boolean, number, ...string[]]

const [a, b, c, ...rest] = tuple;
>a : boolean
>b : number
>c : string
>rest : string[]
>tuple : [boolean, number, ...string[]]

declare var receiver: typeof tuple;
>receiver : [boolean, number, ...string[]]
>tuple : [boolean, number, ...string[]]

[...receiver] = tuple;
>[...receiver] = tuple : [boolean, number, ...string[]]
>[...receiver] : (string | number | boolean)[]
>...receiver : string | number | boolean
>receiver : [boolean, number, ...string[]]
>tuple : [boolean, number, ...string[]]

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var [c1, c2, { c3: c4, c5 }, , ...c6] = [1, 2, { c3: 4, c5: 0 }]; // Error
>c4 : number
>c5 : number
> : undefined
>c6 : any[]
>c6 : []
>[1, 2, { c3: 4, c5: 0 }] : [number, number, { c3: number; c5: number; }, undefined?]
>1 : 1
>2 : 2
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/restElementMustBeLast.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/conformance/types/rest/restElementMustBeLast.ts ===
var [...a, x] = [1, 2, 3]; // Error, rest must be last element
>a : number[]
>a : [number, number, number]
>x : number
>[1, 2, 3] : [number, number, number]
>1 : 1
Expand All @@ -11,7 +11,7 @@ var [...a, x] = [1, 2, 3]; // Error, rest must be last element
>[...a, x] = [1, 2, 3] : number[]
>[...a, x] : number[]
>...a : number
>a : number[]
>a : [number, number, number]
>x : number
>[1, 2, 3] : number[]
>1 : 1
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts(3,10): error TS2322: Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts(3,18): error TS2459: Type '(string | number)[]' has no property 'b' and no string index signature.
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts(3,18): error TS2459: Type '[string, number]' has no property 'b' and no string index signature.


==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts (2 errors) ====
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts (1 errors) ====
var a: string, b: number;
var tuple: [string, number] = ["", 1];
[...{ 0: a = "", b }] = tuple;
~
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
~
!!! error TS2459: Type '(string | number)[]' has no property 'b' and no string index signature.
!!! error TS2459: Type '[string, number]' has no property 'b' and no string index signature.
Loading