Skip to content

Commit 6893a2c

Browse files
Merge branch 'master' into newLineClassification
2 parents 25ed020 + 60a6b28 commit 6893a2c

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,8 +1721,8 @@ module ts {
17211721
}
17221722
}
17231723
else {
1724-
// For an array binding element the specified or inferred type of the parent must be assignable to any[]
1725-
if (!isTypeAssignableTo(parentType, anyArrayType)) {
1724+
// For an array binding element the specified or inferred type of the parent must be an array-like type
1725+
if (!isArrayLikeType(parentType)) {
17261726
error(pattern, Diagnostics.Type_0_is_not_an_array_type, typeToString(parentType));
17271727
return unknownType;
17281728
}
@@ -4190,6 +4190,11 @@ module ts {
41904190
return type.flags & TypeFlags.Reference && (<TypeReference>type).target === globalArrayType;
41914191
}
41924192

4193+
function isArrayLikeType(type: Type): boolean {
4194+
// A type is array-like if it is not the undefined or null type and if it is assignable to any[]
4195+
return !(type.flags & (TypeFlags.Undefined | TypeFlags.Null)) && isTypeAssignableTo(type, anyArrayType);
4196+
}
4197+
41934198
function isTupleLikeType(type: Type): boolean {
41944199
return !!getPropertyOfType(type, "0");
41954200
}
@@ -5466,7 +5471,7 @@ module ts {
54665471

54675472
function checkSpreadElementExpression(node: SpreadElementExpression, contextualMapper?: TypeMapper): Type {
54685473
var type = checkExpressionCached(node.expression, contextualMapper);
5469-
if (!isTypeAssignableTo(type, anyArrayType)) {
5474+
if (!isArrayLikeType(type)) {
54705475
error(node.expression, Diagnostics.Type_0_is_not_an_array_type, typeToString(type));
54715476
return unknownType;
54725477
}
@@ -7074,7 +7079,7 @@ module ts {
70747079

70757080
function checkArrayLiteralAssignment(node: ArrayLiteralExpression, sourceType: Type, contextualMapper?: TypeMapper): Type {
70767081
// TODOO(andersh): Allow iterable source type in ES6
7077-
if (!isTypeAssignableTo(sourceType, anyArrayType)) {
7082+
if (!isArrayLikeType(sourceType)) {
70787083
error(node, Diagnostics.Type_0_is_not_an_array_type, typeToString(sourceType));
70797084
return sourceType;
70807085
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(1,15): error TS2461: Type 'null' is not an array type.
2+
tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(4,15): error TS2461: Type 'undefined' is not an array type.
3+
tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(7,15): error TS2461: Type '{ [x: number]: undefined; }' is not an array type.
4+
5+
6+
==== tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts (3 errors) ====
7+
function foo1([...r] = null) {
8+
~~~~~~
9+
!!! error TS2461: Type 'null' is not an array type.
10+
}
11+
12+
function foo2([...r] = undefined) {
13+
~~~~~~
14+
!!! error TS2461: Type 'undefined' is not an array type.
15+
}
16+
17+
function foo3([...r] = {}) {
18+
~~~~~~
19+
!!! error TS2461: Type '{ [x: number]: undefined; }' is not an array type.
20+
}
21+
22+
function foo4([...r] = []) {
23+
}
24+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [restElementWithNullInitializer.ts]
2+
function foo1([...r] = null) {
3+
}
4+
5+
function foo2([...r] = undefined) {
6+
}
7+
8+
function foo3([...r] = {}) {
9+
}
10+
11+
function foo4([...r] = []) {
12+
}
13+
14+
15+
//// [restElementWithNullInitializer.js]
16+
function foo1(_a) {
17+
var _b = _a === void 0 ? null : _a, r = _b.slice(0);
18+
}
19+
function foo2(_a) {
20+
var _b = _a === void 0 ? undefined : _a, r = _b.slice(0);
21+
}
22+
function foo3(_a) {
23+
var _b = _a === void 0 ? {} : _a, r = _b.slice(0);
24+
}
25+
function foo4(_a) {
26+
var _b = _a === void 0 ? [] : _a, r = _b.slice(0);
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function foo1([...r] = null) {
2+
}
3+
4+
function foo2([...r] = undefined) {
5+
}
6+
7+
function foo3([...r] = {}) {
8+
}
9+
10+
function foo4([...r] = []) {
11+
}

0 commit comments

Comments
 (0)