Merge pull request #2101 from Microsoft/restElementNull

Fix crash on rest element destructuring with null initializer
This commit is contained in:
Anders Hejlsberg
2015-02-21 19:46:35 -08:00
4 changed files with 71 additions and 4 deletions

View File

@@ -1721,8 +1721,8 @@ module ts {
}
}
else {
// For an array binding element the specified or inferred type of the parent must be assignable to any[]
if (!isTypeAssignableTo(parentType, anyArrayType)) {
// For an array binding element the specified or inferred type of the parent must be an array-like type
if (!isArrayLikeType(parentType)) {
error(pattern, Diagnostics.Type_0_is_not_an_array_type, typeToString(parentType));
return unknownType;
}
@@ -4190,6 +4190,11 @@ module ts {
return type.flags & TypeFlags.Reference && (<TypeReference>type).target === globalArrayType;
}
function isArrayLikeType(type: Type): boolean {
// A type is array-like if it is not the undefined or null type and if it is assignable to any[]
return !(type.flags & (TypeFlags.Undefined | TypeFlags.Null)) && isTypeAssignableTo(type, anyArrayType);
}
function isTupleLikeType(type: Type): boolean {
return !!getPropertyOfType(type, "0");
}
@@ -5466,7 +5471,7 @@ module ts {
function checkSpreadElementExpression(node: SpreadElementExpression, contextualMapper?: TypeMapper): Type {
var type = checkExpressionCached(node.expression, contextualMapper);
if (!isTypeAssignableTo(type, anyArrayType)) {
if (!isArrayLikeType(type)) {
error(node.expression, Diagnostics.Type_0_is_not_an_array_type, typeToString(type));
return unknownType;
}
@@ -7074,7 +7079,7 @@ module ts {
function checkArrayLiteralAssignment(node: ArrayLiteralExpression, sourceType: Type, contextualMapper?: TypeMapper): Type {
// TODOO(andersh): Allow iterable source type in ES6
if (!isTypeAssignableTo(sourceType, anyArrayType)) {
if (!isArrayLikeType(sourceType)) {
error(node, Diagnostics.Type_0_is_not_an_array_type, typeToString(sourceType));
return sourceType;
}