Skip to content

Commit f84ebba

Browse files
committed
Rest assignment element allows nested destructuring (fixes microsoft#2156)
1 parent e9f5acc commit f84ebba

17 files changed

Lines changed: 135 additions & 4 deletions

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7634,7 +7634,7 @@ module ts {
76347634
}
76357635
else {
76367636
if (i === elements.length - 1) {
7637-
checkReferenceAssignment((<SpreadElementExpression>e).expression, createArrayType(elementType), contextualMapper);
7637+
checkDestructuringAssignment((<SpreadElementExpression>e).expression, createArrayType(elementType), contextualMapper);
76387638
}
76397639
else {
76407640
error(e, Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern);
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
tests/cases/conformance/es6/for-ofStatements/for-of49.ts(3,13): error TS2364: Invalid left-hand side of assignment expression.
1+
tests/cases/conformance/es6/for-ofStatements/for-of49.ts(3,14): error TS2322: Type 'string | boolean' is not assignable to type 'boolean'.
2+
Type 'string' is not assignable to type 'boolean'.
23

34

45
==== tests/cases/conformance/es6/for-ofStatements/for-of49.ts (1 errors) ====
56
var k: string, v: boolean;
67
var map = new Map([["", true]]);
78
for ([k, ...[v]] of map) {
8-
~~~
9-
!!! error TS2364: Invalid left-hand side of assignment expression.
9+
~
10+
!!! error TS2322: Type 'string | boolean' is not assignable to type 'boolean'.
11+
!!! error TS2322: Type 'string' is not assignable to type 'boolean'.
1012
k;
1113
v;
1214
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern1.ts(2,6): error TS2322: Type 'string | number' is not assignable to type 'string'.
2+
Type 'number' is not assignable to type 'string'.
3+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern1.ts(2,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
4+
Type 'string' is not assignable to type 'number'.
5+
6+
7+
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern1.ts (2 errors) ====
8+
var a: string, b: number;
9+
[...[a, b = 0]] = ["", 1];
10+
~
11+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
12+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
13+
~
14+
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
15+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [restElementWithAssignmentPattern1.ts]
2+
var a: string, b: number;
3+
[...[a, b = 0]] = ["", 1];
4+
5+
//// [restElementWithAssignmentPattern1.js]
6+
var a, b;
7+
[...[a, b = 0]] = ["", 1];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts(2,5): error TS2461: Type '{ 0: string; b: number; }' is not an array type.
2+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts(2,10): error TS2322: Type 'string | number' is not assignable to type 'string'.
3+
Type 'number' is not assignable to type 'string'.
4+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts(2,18): error TS2459: Type '(string | number)[]' has no property 'b' and no string index signature.
5+
6+
7+
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts (3 errors) ====
8+
var a: string, b: number;
9+
[...{ 0: a = "", b }] = ["", 1];
10+
~~~~~~~~~~~~~~~~
11+
!!! error TS2461: Type '{ 0: string; b: number; }' is not an array type.
12+
~
13+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
14+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
15+
~
16+
!!! error TS2459: Type '(string | number)[]' has no property 'b' and no string index signature.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [restElementWithAssignmentPattern2.ts]
2+
var a: string, b: number;
3+
[...{ 0: a = "", b }] = ["", 1];
4+
5+
//// [restElementWithAssignmentPattern2.js]
6+
var a, b;
7+
[...{ 0: a = "", b }] = ["", 1];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern3.ts(3,6): error TS2322: Type 'string | number' is not assignable to type 'string'.
2+
Type 'number' is not assignable to type 'string'.
3+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern3.ts(3,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
4+
Type 'string' is not assignable to type 'number'.
5+
6+
7+
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern3.ts (2 errors) ====
8+
var a: string, b: number;
9+
var tuple: [string, number] = ["", 1];
10+
[...[a, b = 0]] = tuple;
11+
~
12+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
13+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
14+
~
15+
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
16+
!!! error TS2322: Type 'string' is not assignable to type 'number'.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [restElementWithAssignmentPattern3.ts]
2+
var a: string, b: number;
3+
var tuple: [string, number] = ["", 1];
4+
[...[a, b = 0]] = tuple;
5+
6+
//// [restElementWithAssignmentPattern3.js]
7+
var a, b;
8+
var tuple = ["", 1];
9+
[a, b = 0] = tuple.slice(0);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts(3,10): error TS2322: Type 'string | number' is not assignable to type 'string'.
2+
Type 'number' is not assignable to type 'string'.
3+
tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts(3,18): error TS2459: Type '(string | number)[]' has no property 'b' and no string index signature.
4+
5+
6+
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern4.ts (2 errors) ====
7+
var a: string, b: number;
8+
var tuple: [string, number] = ["", 1];
9+
[...{ 0: a = "", b }] = tuple;
10+
~
11+
!!! error TS2322: Type 'string | number' is not assignable to type 'string'.
12+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
13+
~
14+
!!! error TS2459: Type '(string | number)[]' has no property 'b' and no string index signature.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [restElementWithAssignmentPattern4.ts]
2+
var a: string, b: number;
3+
var tuple: [string, number] = ["", 1];
4+
[...{ 0: a = "", b }] = tuple;
5+
6+
//// [restElementWithAssignmentPattern4.js]
7+
var a, b;
8+
var tuple = ["", 1];
9+
{ 0: a = "", b: b } = tuple.slice(0);

0 commit comments

Comments
 (0)