Rest assignment element allows nested destructuring (fixes #2156)

This commit is contained in:
Jason Freeman 2015-04-10 12:11:28 -07:00
parent e9f5acce75
commit f84ebba852
17 changed files with 135 additions and 4 deletions

View File

@ -7634,7 +7634,7 @@ module ts {
}
else {
if (i === elements.length - 1) {
checkReferenceAssignment((<SpreadElementExpression>e).expression, createArrayType(elementType), contextualMapper);
checkDestructuringAssignment((<SpreadElementExpression>e).expression, createArrayType(elementType), contextualMapper);
}
else {
error(e, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern);

View File

@ -1,12 +1,14 @@
tests/cases/conformance/es6/for-ofStatements/for-of49.ts(3,13): error TS2364: Invalid left-hand side of assignment expression.
tests/cases/conformance/es6/for-ofStatements/for-of49.ts(3,14): error TS2322: Type 'string | boolean' is not assignable to type 'boolean'.
Type 'string' is not assignable to type 'boolean'.
==== tests/cases/conformance/es6/for-ofStatements/for-of49.ts (1 errors) ====
var k: string, v: boolean;
var map = new Map([["", true]]);
for ([k, ...[v]] of map) {
~~~
!!! error TS2364: Invalid left-hand side of assignment expression.
~
!!! error TS2322: Type 'string | boolean' is not assignable to type 'boolean'.
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
k;
v;
}

View File

@ -0,0 +1,15 @@
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern1.ts(2,6): error TS2322: Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern1.ts(2,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern1.ts (2 errors) ====
var a: string, b: number;
[...[a, b = 0]] = ["", 1];
~
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
~
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.

View File

@ -0,0 +1,7 @@
//// [restElementWithAssignmentPattern1.ts]
var a: string, b: number;
[...[a, b = 0]] = ["", 1];
//// [restElementWithAssignmentPattern1.js]
var a, b;
[...[a, b = 0]] = ["", 1];

View File

@ -0,0 +1,16 @@
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts(2,5): error TS2461: Type '{ 0: string; b: number; }' is not an array type.
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts(2,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/restElementWithAssignmentPattern2.ts(2,18): error TS2459: Type '(string | number)[]' has no property 'b' and no string index signature.
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts (3 errors) ====
var a: string, b: number;
[...{ 0: a = "", b }] = ["", 1];
~~~~~~~~~~~~~~~~
!!! error TS2461: Type '{ 0: string; b: number; }' is not an array type.
~
!!! 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.

View File

@ -0,0 +1,7 @@
//// [restElementWithAssignmentPattern2.ts]
var a: string, b: number;
[...{ 0: a = "", b }] = ["", 1];
//// [restElementWithAssignmentPattern2.js]
var a, b;
[...{ 0: a = "", b }] = ["", 1];

View File

@ -0,0 +1,16 @@
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern3.ts(3,6): error TS2322: Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern3.ts(3,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern3.ts (2 errors) ====
var a: string, b: number;
var tuple: [string, number] = ["", 1];
[...[a, b = 0]] = tuple;
~
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
!!! error TS2322: Type 'number' is not assignable to type 'string'.
~
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.

View File

@ -0,0 +1,9 @@
//// [restElementWithAssignmentPattern3.ts]
var a: string, b: number;
var tuple: [string, number] = ["", 1];
[...[a, b = 0]] = tuple;
//// [restElementWithAssignmentPattern3.js]
var a, b;
var tuple = ["", 1];
[a, b = 0] = tuple.slice(0);

View File

@ -0,0 +1,14 @@
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 (2 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.

View File

@ -0,0 +1,9 @@
//// [restElementWithAssignmentPattern4.ts]
var a: string, b: number;
var tuple: [string, number] = ["", 1];
[...{ 0: a = "", b }] = tuple;
//// [restElementWithAssignmentPattern4.js]
var a, b;
var tuple = ["", 1];
{ 0: a = "", b: b } = tuple.slice(0);

View File

@ -0,0 +1,8 @@
//// [restElementWithAssignmentPattern5.ts]
var s: string, s2: string;
[...[s, s2]] = ["", ""];
//// [restElementWithAssignmentPattern5.js]
var s, s2;
_a = ["", ""], [s, s2] = _a.slice(0);
var _a;

View File

@ -0,0 +1,14 @@
=== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern5.ts ===
var s: string, s2: string;
>s : string
>s2 : string
[...[s, s2]] = ["", ""];
>[...[s, s2]] = ["", ""] : string[]
>[...[s, s2]] : string[]
>...[s, s2] : string
>[s, s2] : string[]
>s : string
>s2 : string
>["", ""] : string[]

View File

@ -0,0 +1,3 @@
//@target: ES6
var a: string, b: number;
[...[a, b = 0]] = ["", 1];

View File

@ -0,0 +1,3 @@
//@target: ES6
var a: string, b: number;
[...{ 0: a = "", b }] = ["", 1];

View File

@ -0,0 +1,3 @@
var a: string, b: number;
var tuple: [string, number] = ["", 1];
[...[a, b = 0]] = tuple;

View File

@ -0,0 +1,3 @@
var a: string, b: number;
var tuple: [string, number] = ["", 1];
[...{ 0: a = "", b }] = tuple;

View File

@ -0,0 +1,2 @@
var s: string, s2: string;
[...[s, s2]] = ["", ""];