make it work with AssignmentRestElement

This commit is contained in:
Klaus Meinhardt 2018-07-31 10:21:40 +02:00
parent 090326be6a
commit 51b752bbda
7 changed files with 32 additions and 25 deletions

View File

@ -21066,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);
}

View File

@ -2,8 +2,13 @@
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);

View File

@ -9,3 +9,11 @@ const [a, b, c, ...rest] = tuple;
>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))

View File

@ -9,3 +9,14 @@ const [a, b, c, ...rest] = tuple;
>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[]]

View File

@ -1,16 +0,0 @@
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

@ -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.

View File

@ -3,3 +3,7 @@
declare var tuple: [boolean, number, ...string[]];
const [a, b, c, ...rest] = tuple;
declare var receiver: typeof tuple;
[...receiver] = tuple;