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 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
20 changes: 17 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4561,11 +4561,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 @@ -8539,6 +8539,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 @@ -21130,7 +21144,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
50 changes: 26 additions & 24 deletions tests/baselines/reference/declarationsAndAssignments.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,37 +223,39 @@ tests/cases/conformance/es6/destructuring/declarationsAndAssignments.ts(138,9):
var x = ([a, b] = [1, 2]);
}

function f20() {
function f20(v: [number, number, number]) {
var x: number;
var y: number;
var z: number;
var a: number[];
var a3: any[];
var [...a] = [1, 2, 3];
var [x, ...a] = [1, 2, 3];
var [x, y, ...a] = [1, 2, 3];
var [x, y, z, ...a3] = [1, 2, 3];
[...a] = [1, 2, 3];
[x, ...a] = [1, 2, 3];
[x, y, ...a] = [1, 2, 3];
[x, y, z, ...a3] = [1, 2, 3];
var a0: [];
var a1: [number];
var a2: [number, number];
var a3: [number, number, number];
var [...a3] = v;
var [x, ...a2] = v;
var [x, y, ...a1] = v;
var [x, y, z, ...a0] = v;
[...a3] = v;
[x, ...a2] = v;
[x, y, ...a1] = v;
[x, y, z, ...a0] = v;
}

function f21() {
function f21(v: [number, string, boolean]) {
var x: number;
var y: string;
var z: boolean;
var a0: (number | string | boolean)[];
var a1: (string | boolean)[];
var a2: boolean[];
var a3: any[];
var [...a0] = [1, "hello", true];
var [x, ...a1] = [1, "hello", true];
var [x, y, ...a2] = [1, "hello", true];
var [x, y, z, ...a3] = [1, "hello", true];
[...a0] = [1, "hello", true];
[x, ...a1] = [1, "hello", true];
[x, y, ...a2] = [1, "hello", true];
[x, y, z, ...a3] = [1, "hello", true];
var a0: [number, string, boolean];
var a1: [string, boolean];
var a2: [boolean];
var a3: [];
var [...a0] = v;
var [x, ...a1] = v;
var [x, y, ...a2] = v;
var [x, y, z, ...a3] = v;
[...a0] = v;
[x, ...a1] = v;
[x, y, ...a2] = v;
[x, y, z, ...a3] = v;
}

92 changes: 47 additions & 45 deletions tests/baselines/reference/declarationsAndAssignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,38 +149,40 @@ function f19() {
var x = ([a, b] = [1, 2]);
}

function f20() {
function f20(v: [number, number, number]) {
var x: number;
var y: number;
var z: number;
var a: number[];
var a3: any[];
var [...a] = [1, 2, 3];
var [x, ...a] = [1, 2, 3];
var [x, y, ...a] = [1, 2, 3];
var [x, y, z, ...a3] = [1, 2, 3];
[...a] = [1, 2, 3];
[x, ...a] = [1, 2, 3];
[x, y, ...a] = [1, 2, 3];
[x, y, z, ...a3] = [1, 2, 3];
var a0: [];
var a1: [number];
var a2: [number, number];
var a3: [number, number, number];
var [...a3] = v;
var [x, ...a2] = v;
var [x, y, ...a1] = v;
var [x, y, z, ...a0] = v;
[...a3] = v;
[x, ...a2] = v;
[x, y, ...a1] = v;
[x, y, z, ...a0] = v;
}

function f21() {
function f21(v: [number, string, boolean]) {
var x: number;
var y: string;
var z: boolean;
var a0: (number | string | boolean)[];
var a1: (string | boolean)[];
var a2: boolean[];
var a3: any[];
var [...a0] = [1, "hello", true];
var [x, ...a1] = [1, "hello", true];
var [x, y, ...a2] = [1, "hello", true];
var [x, y, z, ...a3] = [1, "hello", true];
[...a0] = [1, "hello", true];
[x, ...a1] = [1, "hello", true];
[x, y, ...a2] = [1, "hello", true];
[x, y, z, ...a3] = [1, "hello", true];
var a0: [number, string, boolean];
var a1: [string, boolean];
var a2: [boolean];
var a3: [];
var [...a0] = v;
var [x, ...a1] = v;
var [x, y, ...a2] = v;
var [x, y, z, ...a3] = v;
[...a0] = v;
[x, ...a1] = v;
[x, y, ...a2] = v;
[x, y, z, ...a3] = v;
}


Expand Down Expand Up @@ -320,37 +322,37 @@ function f19() {
_d = [[2, 3]][0], _e = _d === void 0 ? [1, 2] : _d, a = _e[0], b = _e[1];
var x = (_f = [1, 2], a = _f[0], b = _f[1], _f);
}
function f20() {
var _a, _b, _c;
function f20(v) {
var x;
var y;
var z;
var a;
var a0;
var a1;
var a2;
var a3;
var a = [1, 2, 3].slice(0);
var _d = [1, 2, 3], x = _d[0], a = _d.slice(1);
var _e = [1, 2, 3], x = _e[0], y = _e[1], a = _e.slice(2);
var _f = [1, 2, 3], x = _f[0], y = _f[1], z = _f[2], a3 = _f.slice(3);
a = [1, 2, 3].slice(0);
_a = [1, 2, 3], x = _a[0], a = _a.slice(1);
_b = [1, 2, 3], x = _b[0], y = _b[1], a = _b.slice(2);
_c = [1, 2, 3], x = _c[0], y = _c[1], z = _c[2], a3 = _c.slice(3);
var a3 = v.slice(0);
var x = v[0], a2 = v.slice(1);
var x = v[0], y = v[1], a1 = v.slice(2);
var x = v[0], y = v[1], z = v[2], a0 = v.slice(3);
a3 = v.slice(0);
x = v[0], a2 = v.slice(1);
x = v[0], y = v[1], a1 = v.slice(2);
x = v[0], y = v[1], z = v[2], a0 = v.slice(3);
}
function f21() {
var _a, _b, _c;
function f21(v) {
var x;
var y;
var z;
var a0;
var a1;
var a2;
var a3;
var a0 = [1, "hello", true].slice(0);
var _d = [1, "hello", true], x = _d[0], a1 = _d.slice(1);
var _e = [1, "hello", true], x = _e[0], y = _e[1], a2 = _e.slice(2);
var _f = [1, "hello", true], x = _f[0], y = _f[1], z = _f[2], a3 = _f.slice(3);
a0 = [1, "hello", true].slice(0);
_a = [1, "hello", true], x = _a[0], a1 = _a.slice(1);
_b = [1, "hello", true], x = _b[0], y = _b[1], a2 = _b.slice(2);
_c = [1, "hello", true], x = _c[0], y = _c[1], z = _c[2], a3 = _c.slice(3);
var a0 = v.slice(0);
var x = v[0], a1 = v.slice(1);
var x = v[0], y = v[1], a2 = v.slice(2);
var x = v[0], y = v[1], z = v[2], a3 = v.slice(3);
a0 = v.slice(0);
x = v[0], a1 = v.slice(1);
x = v[0], y = v[1], a2 = v.slice(2);
x = v[0], y = v[1], z = v[2], a3 = v.slice(3);
}
Loading