From ca92653aba44d437476822907493f1386890e5b4 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sat, 21 Feb 2015 19:33:53 -0800 Subject: [PATCH] Addressing CR feedback --- src/compiler/checker.ts | 3 ++- .../restElementWithNullInitializer.errors.txt | 24 +++++++++++++++--- .../restElementWithNullInitializer.js | 25 +++++++++++++++++-- .../restElementWithNullInitializer.ts | 12 ++++++++- 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a06de62be8..01d63c7d85 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1721,7 +1721,7 @@ module ts { } } else { - // For an array binding element the specified or inferred type of the parent must be assignable to any[] + // 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; @@ -4191,6 +4191,7 @@ module ts { } 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); } diff --git a/tests/baselines/reference/restElementWithNullInitializer.errors.txt b/tests/baselines/reference/restElementWithNullInitializer.errors.txt index b1222ebc0e..7beb7b9a64 100644 --- a/tests/baselines/reference/restElementWithNullInitializer.errors.txt +++ b/tests/baselines/reference/restElementWithNullInitializer.errors.txt @@ -1,8 +1,24 @@ -tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(1,14): error TS2461: Type 'null' is not an array type. +tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(1,15): error TS2461: Type 'null' is not an array type. +tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(4,15): error TS2461: Type 'undefined' is not an array type. +tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts(7,15): error TS2461: Type '{ [x: number]: undefined; }' is not an array type. -==== tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts (1 errors) ==== - function foo([...r] = null) { } - ~~~~~~ +==== tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts (3 errors) ==== + function foo1([...r] = null) { + ~~~~~~ !!! error TS2461: Type 'null' is not an array type. + } + + function foo2([...r] = undefined) { + ~~~~~~ +!!! error TS2461: Type 'undefined' is not an array type. + } + + function foo3([...r] = {}) { + ~~~~~~ +!!! error TS2461: Type '{ [x: number]: undefined; }' is not an array type. + } + + function foo4([...r] = []) { + } \ No newline at end of file diff --git a/tests/baselines/reference/restElementWithNullInitializer.js b/tests/baselines/reference/restElementWithNullInitializer.js index a4056c665f..9f326602fb 100644 --- a/tests/baselines/reference/restElementWithNullInitializer.js +++ b/tests/baselines/reference/restElementWithNullInitializer.js @@ -1,6 +1,27 @@ //// [restElementWithNullInitializer.ts] -function foo([...r] = null) { } +function foo1([...r] = null) { +} + +function foo2([...r] = undefined) { +} + +function foo3([...r] = {}) { +} + +function foo4([...r] = []) { +} //// [restElementWithNullInitializer.js] -function foo(_a) { } +function foo1(_a) { + var _b = _a === void 0 ? null : _a, r = _b.slice(0); +} +function foo2(_a) { + var _b = _a === void 0 ? undefined : _a, r = _b.slice(0); +} +function foo3(_a) { + var _b = _a === void 0 ? {} : _a, r = _b.slice(0); +} +function foo4(_a) { + var _b = _a === void 0 ? [] : _a, r = _b.slice(0); +} diff --git a/tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts b/tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts index f136633e09..1b03c533e7 100644 --- a/tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts +++ b/tests/cases/conformance/es6/destructuring/restElementWithNullInitializer.ts @@ -1 +1,11 @@ -function foo([...r] = null) { } +function foo1([...r] = null) { +} + +function foo2([...r] = undefined) { +} + +function foo3([...r] = {}) { +} + +function foo4([...r] = []) { +}