Assignment rest element pretends to get the contextual element type

This commit is contained in:
Jason Freeman
2015-04-10 16:03:54 -07:00
parent f84ebba852
commit 5bfbcdcd25
11 changed files with 92 additions and 7 deletions

View File

@@ -6011,14 +6011,38 @@ module ts {
}
let hasSpreadElement = false;
let elementTypes: Type[] = [];
let inDestructuringPattern = isAssignmentTarget(node);
for (let e of elements) {
let type = checkExpression(e, contextualMapper);
elementTypes.push(type);
if (inDestructuringPattern && e.kind === SyntaxKind.SpreadElementExpression) {
// Given the following situation:
// var c: {};
// [...c] = ["", 0];
//
// c is represented in the tree as a spread element in an array literal.
// But c really functions as a rest element, and its purpose is to provide
// a contextual type for the right hand side of the assignment. Therefore,
// instead of calling checkExpression on "...c", which will give an error
// if c is not iterable/array-like, we need to act as if we are trying to
// get the contextual element type from it. So we do something similar to
// getContextualTypeForElementExpression, which will crucially not error
// if there is no index type / iterated type.
let restArrayType = checkExpression((<SpreadElementExpression>e).expression, contextualMapper);
let restElementType = getIndexTypeOfType(restArrayType, IndexKind.Number) ||
(languageVersion >= ScriptTarget.ES6 ? checkIteratedType(restArrayType, /*expressionForError*/ undefined) : undefined);
if (restElementType) {
elementTypes.push(restElementType);
}
}
else {
let type = checkExpression(e, contextualMapper);
elementTypes.push(type);
}
hasSpreadElement = hasSpreadElement || e.kind === SyntaxKind.SpreadElementExpression;
}
if (!hasSpreadElement) {
let contextualType = getContextualType(node);
if (contextualType && contextualTypeIsTupleLikeType(contextualType) || isAssignmentTarget(node)) {
if (contextualType && contextualTypeIsTupleLikeType(contextualType) || inDestructuringPattern) {
return createTupleType(elementTypes);
}
}

View File

@@ -0,0 +1,8 @@
//// [nonIterableRestElement1.ts]
var c = {};
[...c] = ["", 0];
//// [nonIterableRestElement1.js]
var c = {};
_a = ["", 0], c = _a.slice(0);
var _a;

View File

@@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/destructuring/nonIterableRestElement1.ts ===
var c = {};
>c : {}
>{} : {}
[...c] = ["", 0];
>[...c] = ["", 0] : (string | number)[]
>[...c] : {}[]
>...c : unknown
>c : {}
>["", 0] : (string | number)[]

View File

@@ -0,0 +1,7 @@
//// [nonIterableRestElement2.ts]
var c = {};
[...c] = ["", 0];
//// [nonIterableRestElement2.js]
var c = {};
[...c] = ["", 0];

View File

@@ -0,0 +1,12 @@
=== tests/cases/conformance/es6/destructuring/nonIterableRestElement2.ts ===
var c = {};
>c : {}
>{} : {}
[...c] = ["", 0];
>[...c] = ["", 0] : (string | number)[]
>[...c] : {}[]
>...c : any
>c : {}
>["", 0] : (string | number)[]

View File

@@ -0,0 +1,10 @@
tests/cases/conformance/es6/destructuring/nonIterableRestElement3.ts(2,5): error TS2322: Type '(string | number)[]' is not assignable to type '{ bogus: number; }'.
Property 'bogus' is missing in type '(string | number)[]'.
==== tests/cases/conformance/es6/destructuring/nonIterableRestElement3.ts (1 errors) ====
var c = { bogus: 0 };
[...c] = ["", 0];
~
!!! error TS2322: Type '(string | number)[]' is not assignable to type '{ bogus: number; }'.
!!! error TS2322: Property 'bogus' is missing in type '(string | number)[]'.

View File

@@ -0,0 +1,8 @@
//// [nonIterableRestElement3.ts]
var c = { bogus: 0 };
[...c] = ["", 0];
//// [nonIterableRestElement3.js]
var c = { bogus: 0 };
_a = ["", 0], c = _a.slice(0);
var _a;

View File

@@ -1,14 +1,11 @@
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) ====
==== tests/cases/conformance/es6/destructuring/restElementWithAssignmentPattern2.ts (2 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'.

View File

@@ -0,0 +1,2 @@
var c = {};
[...c] = ["", 0];

View File

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

View File

@@ -0,0 +1,2 @@
var c = { bogus: 0 };
[...c] = ["", 0];