From 5039b4becbb72e2c9693b898541f3e329d01e2c8 Mon Sep 17 00:00:00 2001 From: Yui T Date: Sun, 12 Apr 2015 16:03:16 -0700 Subject: [PATCH 1/6] Conformance test for 3.8.2.2 and 6.4 --- ...structuringParameterDeclaration.errors.txt | 37 ++++++++ .../destructuringParameterDeclaration.js | 58 ++++++++++++ ...tructuringParameterDeclaration1.errors.txt | 48 ++++++++++ .../destructuringParameterDeclaration1.js | 86 +++++++++++++++++ ...cturingParameterDeclaration1ES6.errors.txt | 48 ++++++++++ .../destructuringParameterDeclaration1ES6.js | 39 ++++++++ ...tructuringParameterDeclaration2.errors.txt | 56 +++++++++++ .../destructuringParameterDeclaration2.js | 65 +++++++++++++ ...cturingParameterDeclaration2ES6.errors.txt | 56 +++++++++++ .../destructuringParameterDeclaration2ES6.js | 42 +++++++++ .../destructuringParameterDeclaration3.js | 51 ++++++++++ .../destructuringParameterDeclaration3.types | 92 +++++++++++++++++++ .../destructuringParameterDeclaration3ES6.js | 45 +++++++++ ...estructuringParameterDeclaration3ES6.types | 92 +++++++++++++++++++ ...ucturingParameterDeclarationES6.errors.txt | 37 ++++++++ .../destructuringParameterDeclarationES6.js | 44 +++++++++ .../destructuringParameterDeclaration.ts | 19 ++++ .../destructuringParameterDeclaration1.ts | 21 +++++ .../destructuringParameterDeclaration1ES6.ts | 22 +++++ .../destructuringParameterDeclaration2.ts | 19 ++++ .../destructuringParameterDeclaration2ES6.ts | 20 ++++ .../destructuringParameterDeclaration3.ts | 23 +++++ .../destructuringParameterDeclaration3ES6.ts | 24 +++++ .../destructuringParameterDeclarationES6.ts | 20 ++++ 24 files changed, 1064 insertions(+) create mode 100644 tests/baselines/reference/destructuringParameterDeclaration.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration2.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration2.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration2ES6.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration2ES6.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3.types create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES6.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES6.types create mode 100644 tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclarationES6.js create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts diff --git a/tests/baselines/reference/destructuringParameterDeclaration.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration.errors.txt new file mode 100644 index 0000000000000..643305a0167f1 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration.errors.txt @@ -0,0 +1,37 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(17,1): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(18,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. + Property 'z' is missing in type '{}'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(19,14): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(19,18): error TS2300: Duplicate identifier 'z'. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts (4 errors) ==== + enum Foo { a } + function a({x, a}: { x: number, a: number }) { } + function a1({z: {x, y: {j}}}) { } + function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + function a3({z} = {z:10}) { } + function a4({z=10}) { } + function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } + a2(); + a2({ z: { x: "hello" , y: { j: Foo.a } }}); + a3(); + a3({ z: Foo.a }); + a4({}); + a6({ b: 10 }); + a6({ b: true }); + + // error + a4(); + ~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + a3({}); + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. +!!! error TS2345: Property 'z' is missing in type '{}'. + function a5([z], z: number) { } + ~ +!!! error TS2300: Duplicate identifier 'z'. + ~ +!!! error TS2300: Duplicate identifier 'z'. + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration.js b/tests/baselines/reference/destructuringParameterDeclaration.js new file mode 100644 index 0000000000000..da979430ff320 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration.js @@ -0,0 +1,58 @@ +//// [destructuringParameterDeclaration.ts] +enum Foo { a } +function a({x, a}: { x: number, a: number }) { } +function a1({z: {x, y: {j}}}) { } +function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +function a3({z} = {z:10}) { } +function a4({z=10}) { } +function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } +a2(); +a2({ z: { x: "hello" , y: { j: Foo.a } }}); +a3(); +a3({ z: Foo.a }); +a4({}); +a6({ b: 10 }); +a6({ b: true }); + +// error +a4(); +a3({}); +function a5([z], z: number) { } + + +//// [destructuringParameterDeclaration.js] +var Foo; +(function (Foo) { + Foo[Foo["a"] = 0] = "a"; +})(Foo || (Foo = {})); +function a(_a) { + var x = _a.x, a = _a.a; +} +function a1(_a) { + var _b = _a.z, x = _b.x, j = _b.y.j; +} +function a2(_a) { + var _b = (_a === void 0 ? { z: { x: "hi", y: { j: 1 } } } : _a).z, x = _b.x, j = _b.y.j; +} +function a3(_a) { + var z = (_a === void 0 ? { z: 10 } : _a).z; +} +function a4(_a) { + var _b = _a.z, z = _b === void 0 ? 10 : _b; +} +function a6(_a) { + var b = (_a === void 0 ? { b: "hello" } : _a).b; +} +a2(); +a2({ z: { x: "hello", y: { j: Foo.a } } }); +a3(); +a3({ z: Foo.a }); +a4({}); +a6({ b: 10 }); +a6({ b: true }); +// error +a4(); +a3({}); +function a5(_a, z) { + var z = _a[0]; +} diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt new file mode 100644 index 0000000000000..2651be05038d2 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt @@ -0,0 +1,48 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(5,16): error TS1048: A rest parameter cannot have an initializer. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(11,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(12,18): error TS1047: A rest parameter cannot be optional. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(13,17): error TS1048: A rest parameter cannot have an initializer. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(16,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(21,4): error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. + Type 'boolean' is not assignable to type 'string | number'. + Type 'boolean' is not assignable to type 'number'. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts (6 errors) ==== + type arrayString = Array + type someArray = Array | number[]; + type stringOrNumArray = Array; + + function a3(...x = [1,2,3]) { } + ~ +!!! error TS1048: A rest parameter cannot have an initializer. + function a4(...x: (number|string)[]) { } + function a5(...a) { } + function a6(...a: Array) { } + function a7(...a: arrayString) { } + function a8(...a: stringOrNumArray) { } + function a9(...a: someArray) { } + ~~~~~~~~~~~~~~~ +!!! error TS2370: A rest parameter must be of an array type. + function a10(...b?) { } + ~ +!!! error TS1047: A rest parameter cannot be optional. + function a11(...b = [1,2,3]) { } + ~ +!!! error TS1048: A rest parameter cannot have an initializer. + + + a4(1, 2, "hello", true); + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + var array = [1, 2, 3]; + var array2 = [true, false, "hello"]; + a5([...array]); + a4(...array); + a4(...array2); + ~~~~~~~~~ +!!! error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.js b/tests/baselines/reference/destructuringParameterDeclaration1.js new file mode 100644 index 0000000000000..37967ddb4415f --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1.js @@ -0,0 +1,86 @@ +//// [destructuringParameterDeclaration1.ts] +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a3(...x = [1,2,3]) { } +function a4(...x: (number|string)[]) { } +function a5(...a) { } +function a6(...a: Array) { } +function a7(...a: arrayString) { } +function a8(...a: stringOrNumArray) { } +function a9(...a: someArray) { } +function a10(...b?) { } +function a11(...b = [1,2,3]) { } + + +a4(1, 2, "hello", true); +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a5([...array]); +a4(...array); +a4(...array2); + +//// [destructuringParameterDeclaration1.js] +function a3() { + if (x === void 0) { x = [1, 2, 3]; } + var x = []; + for (var _i = 0; _i < arguments.length; _i++) { + x[_i - 0] = arguments[_i]; + } +} +function a4() { + var x = []; + for (var _i = 0; _i < arguments.length; _i++) { + x[_i - 0] = arguments[_i]; + } +} +function a5() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +function a6() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +function a7() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +function a8() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +function a9() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +function a10() { + var b = []; + for (var _i = 0; _i < arguments.length; _i++) { + b[_i - 0] = arguments[_i]; + } +} +function a11() { + if (b === void 0) { b = [1, 2, 3]; } + var b = []; + for (var _i = 0; _i < arguments.length; _i++) { + b[_i - 0] = arguments[_i]; + } +} +a4(1, 2, "hello", true); +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a5(array); +a4.apply(void 0, array); +a4.apply(void 0, array2); diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt new file mode 100644 index 0000000000000..786571c88a587 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt @@ -0,0 +1,48 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(5,16): error TS1048: A rest parameter cannot have an initializer. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(11,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(12,18): error TS1047: A rest parameter cannot be optional. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(13,17): error TS1048: A rest parameter cannot have an initializer. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(16,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(21,4): error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. + Type 'boolean' is not assignable to type 'string | number'. + Type 'boolean' is not assignable to type 'number'. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts (6 errors) ==== + type arrayString = Array + type someArray = Array | number[]; + type stringOrNumArray = Array; + + function a3(...x = [1,2,3]) { } + ~ +!!! error TS1048: A rest parameter cannot have an initializer. + function a4(...x: (number|string)[]) { } + function a5(...a) { } + function a6(...a: Array) { } + function a7(...a: arrayString) { } + function a8(...a: stringOrNumArray) { } + function a9(...a: someArray) { } + ~~~~~~~~~~~~~~~ +!!! error TS2370: A rest parameter must be of an array type. + function a10(...b?) { } + ~ +!!! error TS1047: A rest parameter cannot be optional. + function a11(...b = [1,2,3]) { } + ~ +!!! error TS1048: A rest parameter cannot have an initializer. + + + a4(1, 2, "hello", true); + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + var array = [1, 2, 3]; + var array2 = [true, false, "hello"]; + a5([...array]); + a4(...array); + a4(...array2); + ~~~~~~~~~ +!!! error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.js b/tests/baselines/reference/destructuringParameterDeclaration1ES6.js new file mode 100644 index 0000000000000..779f5495f5682 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES6.js @@ -0,0 +1,39 @@ +//// [destructuringParameterDeclaration1ES6.ts] +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a3(...x = [1,2,3]) { } +function a4(...x: (number|string)[]) { } +function a5(...a) { } +function a6(...a: Array) { } +function a7(...a: arrayString) { } +function a8(...a: stringOrNumArray) { } +function a9(...a: someArray) { } +function a10(...b?) { } +function a11(...b = [1,2,3]) { } + + +a4(1, 2, "hello", true); +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a5([...array]); +a4(...array); +a4(...array2); + +//// [destructuringParameterDeclaration1ES6.js] +function a3(...x = [1, 2, 3]) { } +function a4(...x) { } +function a5(...a) { } +function a6(...a) { } +function a7(...a) { } +function a8(...a) { } +function a9(...a) { } +function a10(...b) { } +function a11(...b = [1, 2, 3]) { } +a4(1, 2, "hello", true); +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a5([...array]); +a4(...array); +a4(...array2); diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt new file mode 100644 index 0000000000000..afe984cc03194 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt @@ -0,0 +1,56 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,14): error TS1181: Array element destructuring pattern expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,19): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,21): error TS1109: Expression expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,24): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,26): error TS2304: Cannot find name 'public'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,32): error TS1005: ';' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,33): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,16): error TS1003: Identifier expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,21): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(9,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(12,24): error TS1005: ',' expected. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts (11 errors) ==== + "use strict" + function a({while}) { } + function a1({public}) { } + function a2({public: x}) { } + function a3({while: y}) { } + function a4([while, for, public]){ } + ~~~~~ +!!! error TS1181: Array element destructuring pattern expected. + ~ +!!! error TS1005: '(' expected. + ~~~ +!!! error TS1109: Expression expected. + ~ +!!! error TS1005: '(' expected. + ~~~~~~ +!!! error TS2304: Cannot find name 'public'. + ~ +!!! error TS1005: ';' expected. + ~ +!!! error TS1128: Declaration or statement expected. + function a5(...while) { } + ~~~~~ +!!! error TS1003: Identifier expected. + ~ +!!! error TS1005: '(' expected. + function a6(...public) { } + function a7(...a: string) { } + ~~~~~~~~~~~~ +!!! error TS2370: A rest parameter must be of an array type. + + class C { + constructor(public ...a) { } + ~~~ +!!! error TS1005: ',' expected. + } + + + a2({ public: 1 }); + a3({ while: 1 }); + a({ while: 1 }); + + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.js b/tests/baselines/reference/destructuringParameterDeclaration2.js new file mode 100644 index 0000000000000..ba6d754844215 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration2.js @@ -0,0 +1,65 @@ +//// [destructuringParameterDeclaration2.ts] +"use strict" +function a({while}) { } +function a1({public}) { } +function a2({public: x}) { } +function a3({while: y}) { } +function a4([while, for, public]){ } +function a5(...while) { } +function a6(...public) { } +function a7(...a: string) { } + +class C { + constructor(public ...a) { } +} + + +a2({ public: 1 }); +a3({ while: 1 }); +a({ while: 1 }); + + + +//// [destructuringParameterDeclaration2.js] +"use strict"; +function a(_a) { + var while = _a.while; +} +function a1(_a) { + var public = _a.public; +} +function a2(_a) { + var x = _a.public; +} +function a3(_a) { + var y = _a.while; +} +while (, ) + for (, public; ; ) + ; +{ } +while () { } +function a6() { + var public = []; + for (var _i = 0; _i < arguments.length; _i++) { + public[_i - 0] = arguments[_i]; + } +} +function a7() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +var C = (function () { + function C(public) { + var a = []; + for (var _i = 1; _i < arguments.length; _i++) { + a[_i - 1] = arguments[_i]; + } + } + return C; +})(); +a2({ public: 1 }); +a3({ while: 1 }); +a({ while: 1 }); diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration2ES6.errors.txt new file mode 100644 index 0000000000000..762a0e1dd8a18 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration2ES6.errors.txt @@ -0,0 +1,56 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,14): error TS1181: Array element destructuring pattern expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,19): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,21): error TS1109: Expression expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,24): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,26): error TS2304: Cannot find name 'public'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,32): error TS1005: ';' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,33): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(7,16): error TS1003: Identifier expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(7,21): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(9,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(12,24): error TS1005: ',' expected. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts (11 errors) ==== + "use strict" + function a({while}) { } + function a1({public}) { } + function a2({public: x}) { } + function a3({while: y}) { } + function a4([while, for, public]){ } + ~~~~~ +!!! error TS1181: Array element destructuring pattern expected. + ~ +!!! error TS1005: '(' expected. + ~~~ +!!! error TS1109: Expression expected. + ~ +!!! error TS1005: '(' expected. + ~~~~~~ +!!! error TS2304: Cannot find name 'public'. + ~ +!!! error TS1005: ';' expected. + ~ +!!! error TS1128: Declaration or statement expected. + function a5(...while) { } + ~~~~~ +!!! error TS1003: Identifier expected. + ~ +!!! error TS1005: '(' expected. + function a6(...public) { } + function a7(...a: string) { } + ~~~~~~~~~~~~ +!!! error TS2370: A rest parameter must be of an array type. + + class C { + constructor(public ...a) { } + ~~~ +!!! error TS1005: ',' expected. + } + + + a2({ public: 1 }); + a3({ while: 1 }); + a({ while: 1 }); + + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.js b/tests/baselines/reference/destructuringParameterDeclaration2ES6.js new file mode 100644 index 0000000000000..6e86aa2c001e0 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration2ES6.js @@ -0,0 +1,42 @@ +//// [destructuringParameterDeclaration2ES6.ts] +"use strict" +function a({while}) { } +function a1({public}) { } +function a2({public: x}) { } +function a3({while: y}) { } +function a4([while, for, public]){ } +function a5(...while) { } +function a6(...public) { } +function a7(...a: string) { } + +class C { + constructor(public ...a) { } +} + + +a2({ public: 1 }); +a3({ while: 1 }); +a({ while: 1 }); + + + +//// [destructuringParameterDeclaration2ES6.js] +"use strict"; +function a({ while }) { } +function a1({ public }) { } +function a2({ public: x }) { } +function a3({ while: y }) { } +while (, ) + for (, public; ; ) + ; +{ } +while () { } +function a6(...public) { } +function a7(...a) { } +class C { + constructor(public, ...a) { + } +} +a2({ public: 1 }); +a3({ while: 1 }); +a({ while: 1 }); diff --git a/tests/baselines/reference/destructuringParameterDeclaration3.js b/tests/baselines/reference/destructuringParameterDeclaration3.js new file mode 100644 index 0000000000000..82d1404881c97 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3.js @@ -0,0 +1,51 @@ +//// [destructuringParameterDeclaration3.ts] +function a([x, [y, z], [[j]]]) { + return [x, y, z, j]; +} + +function a1([...x]) { + return [x]; +} + +function a2({public} = { "public": "1" }) { + return public; +} + +function a3({x: { y, z}, j: {k: {a}} }) { + return [y, z, a]; +} + +function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { + return [y, z, a]; +} + +function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { + return [y, z, a]; +} + + +//// [destructuringParameterDeclaration3.js] +function a(_a) { + var x = _a[0], _b = _a[1], y = _b[0], z = _b[1], j = _a[2][0][0]; + return [x, y, z, j]; +} +function a1(_a) { + var x = _a.slice(0); + return [x]; +} +function a2(_a) { + var public = (_a === void 0 ? { "public": "1" } : _a).public; + return public; +} +function a3(_a) { + var _b = _a.x, y = _b.y, z = _b.z, a = _a.j.k.a; + return [y, z, a]; +} +function a4(_a) { + var _b = _a === void 0 ? { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } } : _a, _c = _b.x, y = _c.y, z = _c.z, a = _b.j.k.a; + return [y, z, a]; +} +function a5(_a) { + var _b = _a.x, y = _b.y, z = _b.z, a = _a.j.k.a; + return [y, z, a]; +} diff --git a/tests/baselines/reference/destructuringParameterDeclaration3.types b/tests/baselines/reference/destructuringParameterDeclaration3.types new file mode 100644 index 0000000000000..41835d9e456c5 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3.types @@ -0,0 +1,92 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts === +function a([x, [y, z], [[j]]]) { +>a : ([x, [y, z], [[j]]]: [any, [any, any], [[any]]]) => any[] +>x : any +>y : any +>z : any +>j : any + + return [x, y, z, j]; +>[x, y, z, j] : any[] +>x : any +>y : any +>z : any +>j : any +} + +function a1([...x]) { +>a1 : ([...x]: any[]) => any[][] +>x : any[] + + return [x]; +>[x] : any[][] +>x : any[] +} + +function a2({public} = { "public": "1" }) { +>a2 : ({public}?: { "public": string; }) => string +>public : string +>{ "public": "1" } : { "public": string; } + + return public; +>public : string +} + +function a3({x: { y, z}, j: {k: {a}} }) { +>a3 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => any[] +>x : unknown +>y : any +>z : any +>j : unknown +>k : unknown +>a : any + + return [y, z, a]; +>[y, z, a] : any[] +>y : any +>z : any +>a : any +} + +function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { +>a4 : ({x: { y, z}, j: {k: {a}} }?: { x: { y: number; z: number; }; j: { k: { a: string; }; }; }) => (string | number)[] +>x : unknown +>y : number +>z : number +>j : unknown +>k : unknown +>a : string +>{ x: { y: 1, z: 1 }, j: { k: { a: "hello" } } } : { x: { y: number; z: number; }; j: { k: { a: string; }; }; } +>x : { y: number; z: number; } +>{ y: 1, z: 1 } : { y: number; z: number; } +>y : number +>z : number +>j : { k: { a: string; }; } +>{ k: { a: "hello" } } : { k: { a: string; }; } +>k : { a: string; } +>{ a: "hello" } : { a: string; } +>a : string + + return [y, z, a]; +>[y, z, a] : (string | number)[] +>y : number +>z : number +>a : string +} + +function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { +>a5 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => (string | number)[] +>x : unknown +>y : any +>z : any +>j : unknown +>k : unknown +>a : any + + return [y, z, a]; +>[y, z, a] : any[] +>y : any +>z : any +>a : any +} + diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES6.js b/tests/baselines/reference/destructuringParameterDeclaration3ES6.js new file mode 100644 index 0000000000000..e129140f3f29f --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES6.js @@ -0,0 +1,45 @@ +//// [destructuringParameterDeclaration3ES6.ts] +function a([x, [y, z], [[j]]]) { + return [x, y, z, j]; +} + +function a1([...x]) { + return [x]; +} + +function a2({public} = { "public": "1" }) { + return public; +} + +function a3({x: { y, z}, j: {k: {a}} }) { + return [y, z, a]; +} + +function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { + return [y, z, a]; +} + +function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { + return [y, z, a]; +} + + +//// [destructuringParameterDeclaration3ES6.js] +function a([x, [y, z], [[j]]]) { + return [x, y, z, j]; +} +function a1([...x]) { + return [x]; +} +function a2({ public } = { "public": "1" }) { + return public; +} +function a3({ x: { y, z }, j: { k: { a } } }) { + return [y, z, a]; +} +function a4({ x: { y, z }, j: { k: { a } } } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }) { + return [y, z, a]; +} +function a5({ x: { y, z }, j: { k: { a } } }) { + return [y, z, a]; +} diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES6.types b/tests/baselines/reference/destructuringParameterDeclaration3ES6.types new file mode 100644 index 0000000000000..01c67e1e547ca --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES6.types @@ -0,0 +1,92 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts === +function a([x, [y, z], [[j]]]) { +>a : ([x, [y, z], [[j]]]: [any, [any, any], [[any]]]) => any[] +>x : any +>y : any +>z : any +>j : any + + return [x, y, z, j]; +>[x, y, z, j] : any[] +>x : any +>y : any +>z : any +>j : any +} + +function a1([...x]) { +>a1 : ([...x]: Iterable) => any[][] +>x : any[] + + return [x]; +>[x] : any[][] +>x : any[] +} + +function a2({public} = { "public": "1" }) { +>a2 : ({public}?: { "public": string; }) => string +>public : string +>{ "public": "1" } : { "public": string; } + + return public; +>public : string +} + +function a3({x: { y, z}, j: {k: {a}} }) { +>a3 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => any[] +>x : unknown +>y : any +>z : any +>j : unknown +>k : unknown +>a : any + + return [y, z, a]; +>[y, z, a] : any[] +>y : any +>z : any +>a : any +} + +function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { +>a4 : ({x: { y, z}, j: {k: {a}} }?: { x: { y: number; z: number; }; j: { k: { a: string; }; }; }) => (string | number)[] +>x : unknown +>y : number +>z : number +>j : unknown +>k : unknown +>a : string +>{ x: { y: 1, z: 1 }, j: { k: { a: "hello" } } } : { x: { y: number; z: number; }; j: { k: { a: string; }; }; } +>x : { y: number; z: number; } +>{ y: 1, z: 1 } : { y: number; z: number; } +>y : number +>z : number +>j : { k: { a: string; }; } +>{ k: { a: "hello" } } : { k: { a: string; }; } +>k : { a: string; } +>{ a: "hello" } : { a: string; } +>a : string + + return [y, z, a]; +>[y, z, a] : (string | number)[] +>y : number +>z : number +>a : string +} + +function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { +>a5 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => (string | number)[] +>x : unknown +>y : any +>z : any +>j : unknown +>k : unknown +>a : any + + return [y, z, a]; +>[y, z, a] : any[] +>y : any +>z : any +>a : any +} + diff --git a/tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt b/tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt new file mode 100644 index 0000000000000..21cd9f36c0fdb --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt @@ -0,0 +1,37 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(17,1): error TS2346: Supplied parameters do not match any signature of call target. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(18,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. + Property 'z' is missing in type '{}'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(19,14): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(19,18): error TS2300: Duplicate identifier 'z'. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts (4 errors) ==== + enum Foo { a } + function a({x, a}: { x: number, a: number }) { } + function a1({z: {x, y: {j}}}) { } + function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + function a3({z} = {z:10}) { } + function a4({z=10}) { } + function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } + a2(); + a2({ z: { x: "hello" , y: { j: Foo.a } }}); + a3(); + a3({ z: Foo.a }); + a4({}); + a6({ b: 10 }); + a6({ b: true }); + + // error + a4(); + ~~~~ +!!! error TS2346: Supplied parameters do not match any signature of call target. + a3({}); + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. +!!! error TS2345: Property 'z' is missing in type '{}'. + function a5([z], z: number) { } + ~ +!!! error TS2300: Duplicate identifier 'z'. + ~ +!!! error TS2300: Duplicate identifier 'z'. + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclarationES6.js b/tests/baselines/reference/destructuringParameterDeclarationES6.js new file mode 100644 index 0000000000000..7a4c929003d60 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclarationES6.js @@ -0,0 +1,44 @@ +//// [destructuringParameterDeclarationES6.ts] +enum Foo { a } +function a({x, a}: { x: number, a: number }) { } +function a1({z: {x, y: {j}}}) { } +function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +function a3({z} = {z:10}) { } +function a4({z=10}) { } +function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } +a2(); +a2({ z: { x: "hello" , y: { j: Foo.a } }}); +a3(); +a3({ z: Foo.a }); +a4({}); +a6({ b: 10 }); +a6({ b: true }); + +// error +a4(); +a3({}); +function a5([z], z: number) { } + + +//// [destructuringParameterDeclarationES6.js] +var Foo; +(function (Foo) { + Foo[Foo["a"] = 0] = "a"; +})(Foo || (Foo = {})); +function a({ x, a }) { } +function a1({ z: { x, y: { j } } }) { } +function a2({ z: { x, y: { j } } } = { z: { x: "hi", y: { j: 1 } } }) { } +function a3({ z } = { z: 10 }) { } +function a4({ z = 10 }) { } +function a6({ b } = { b: "hello" }) { } +a2(); +a2({ z: { x: "hello", y: { j: Foo.a } } }); +a3(); +a3({ z: Foo.a }); +a4({}); +a6({ b: 10 }); +a6({ b: true }); +// error +a4(); +a3({}); +function a5([z], z) { } diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts new file mode 100644 index 0000000000000..c840dba2d2b62 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts @@ -0,0 +1,19 @@ +enum Foo { a } +function a({x, a}: { x: number, a: number }) { } +function a1({z: {x, y: {j}}}) { } +function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +function a3({z} = {z:10}) { } +function a4({z=10}) { } +function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } +a2(); +a2({ z: { x: "hello" , y: { j: Foo.a } }}); +a3(); +a3({ z: Foo.a }); +a4({}); +a6({ b: 10 }); +a6({ b: true }); + +// error +a4(); +a3({}); +function a5([z], z: number) { } diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts new file mode 100644 index 0000000000000..6e9579926dd44 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts @@ -0,0 +1,21 @@ +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a3(...x = [1,2,3]) { } +function a4(...x: (number|string)[]) { } +function a5(...a) { } +function a6(...a: Array) { } +function a7(...a: arrayString) { } +function a8(...a: stringOrNumArray) { } +function a9(...a: someArray) { } +function a10(...b?) { } +function a11(...b = [1,2,3]) { } + + +a4(1, 2, "hello", true); +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a5([...array]); +a4(...array); +a4(...array2); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts new file mode 100644 index 0000000000000..1441226e061f6 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts @@ -0,0 +1,22 @@ +// @target: es6 +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a3(...x = [1,2,3]) { } +function a4(...x: (number|string)[]) { } +function a5(...a) { } +function a6(...a: Array) { } +function a7(...a: arrayString) { } +function a8(...a: stringOrNumArray) { } +function a9(...a: someArray) { } +function a10(...b?) { } +function a11(...b = [1,2,3]) { } + + +a4(1, 2, "hello", true); +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a5([...array]); +a4(...array); +a4(...array2); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts new file mode 100644 index 0000000000000..59763c959027a --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts @@ -0,0 +1,19 @@ +"use strict" +function a({while}) { } +function a1({public}) { } +function a2({public: x}) { } +function a3({while: y}) { } +function a4([while, for, public]){ } +function a5(...while) { } +function a6(...public) { } +function a7(...a: string) { } + +class C { + constructor(public ...a) { } +} + + +a2({ public: 1 }); +a3({ while: 1 }); +a({ while: 1 }); + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts new file mode 100644 index 0000000000000..e50a455d8241d --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts @@ -0,0 +1,20 @@ +//@target: es6 +"use strict" +function a({while}) { } +function a1({public}) { } +function a2({public: x}) { } +function a3({while: y}) { } +function a4([while, for, public]){ } +function a5(...while) { } +function a6(...public) { } +function a7(...a: string) { } + +class C { + constructor(public ...a) { } +} + + +a2({ public: 1 }); +a3({ while: 1 }); +a({ while: 1 }); + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts new file mode 100644 index 0000000000000..96fc87c6c86ac --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts @@ -0,0 +1,23 @@ +function a([x, [y, z], [[j]]]) { + return [x, y, z, j]; +} + +function a1([...x]) { + return [x]; +} + +function a2({public} = { "public": "1" }) { + return public; +} + +function a3({x: { y, z}, j: {k: {a}} }) { + return [y, z, a]; +} + +function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { + return [y, z, a]; +} + +function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { + return [y, z, a]; +} diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts new file mode 100644 index 0000000000000..7c50917ebe5f7 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts @@ -0,0 +1,24 @@ +// @target: es6 +function a([x, [y, z], [[j]]]) { + return [x, y, z, j]; +} + +function a1([...x]) { + return [x]; +} + +function a2({public} = { "public": "1" }) { + return public; +} + +function a3({x: { y, z}, j: {k: {a}} }) { + return [y, z, a]; +} + +function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { + return [y, z, a]; +} + +function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { + return [y, z, a]; +} diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts new file mode 100644 index 0000000000000..0d3594218517d --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts @@ -0,0 +1,20 @@ +//@target: es6 +enum Foo { a } +function a({x, a}: { x: number, a: number }) { } +function a1({z: {x, y: {j}}}) { } +function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +function a3({z} = {z:10}) { } +function a4({z=10}) { } +function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } +a2(); +a2({ z: { x: "hello" , y: { j: Foo.a } }}); +a3(); +a3({ z: Foo.a }); +a4({}); +a6({ b: 10 }); +a6({ b: true }); + +// error +a4(); +a3({}); +function a5([z], z: number) { } From b29077a19baee97a8d86e2c7851953569652918e Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 13 Apr 2015 12:57:50 -0700 Subject: [PATCH 2/6] Add destructuring parameter with generic --- ...tructuringParameterDeclaration4.errors.txt | 27 +++++++++ .../destructuringParameterDeclaration4.js | 58 +++++++++++++++++++ .../destructuringParameterDeclaration4.ts | 18 ++++++ 3 files changed, 103 insertions(+) create mode 100644 tests/baselines/reference/destructuringParameterDeclaration4.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration4.js create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts diff --git a/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt new file mode 100644 index 0000000000000..cf4b15d12714b --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(15,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. + Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts (1 errors) ==== + interface F { } + class C implements F{ + } + function foo(...a: T[]) { } + function foo1(...a: T[]) { } + function bar({x} = { x: new C() }) { } + function baz({x}: { x: F }) { } + function baz1({x}: { x: C }) { } + function baz2({x}: { x: C }) { } + + var obj = new C(); + baz1({ x: obj }); + baz({ x: new C() }); + baz({ x: {} }); + foo("hello", 1, 2); + ~~~ +!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. +!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. + foo("hello", 1, 2); + foo("hello", "world"); + + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration4.js b/tests/baselines/reference/destructuringParameterDeclaration4.js new file mode 100644 index 0000000000000..3f83402dffabc --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration4.js @@ -0,0 +1,58 @@ +//// [destructuringParameterDeclaration4.ts] +interface F { } +class C implements F{ +} +function foo(...a: T[]) { } +function foo1(...a: T[]) { } +function bar({x} = { x: new C() }) { } +function baz({x}: { x: F }) { } +function baz1({x}: { x: C }) { } +function baz2({x}: { x: C }) { } + +var obj = new C(); +baz1({ x: obj }); +baz({ x: new C() }); +baz({ x: {} }); +foo("hello", 1, 2); +foo("hello", 1, 2); +foo("hello", "world"); + + + +//// [destructuringParameterDeclaration4.js] +var C = (function () { + function C() { + } + return C; +})(); +function foo() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +function foo1() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +function bar(_a) { + var x = (_a === void 0 ? { x: new C() } : _a).x; +} +function baz(_a) { + var x = _a.x; +} +function baz1(_a) { + var x = _a.x; +} +function baz2(_a) { + var x = _a.x; +} +var obj = new C(); +baz1({ x: obj }); +baz({ x: new C() }); +baz({ x: {} }); +foo("hello", 1, 2); +foo("hello", 1, 2); +foo("hello", "world"); diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts new file mode 100644 index 0000000000000..b59c539d672b5 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts @@ -0,0 +1,18 @@ +interface F { } +class C implements F{ +} +function foo(...a: T[]) { } +function foo1(...a: T[]) { } +function bar({x} = { x: new C() }) { } +function baz({x}: { x: F }) { } +function baz1({x}: { x: C }) { } +function baz2({x}: { x: C }) { } + +var obj = new C(); +baz1({ x: obj }); +baz({ x: new C() }); +baz({ x: {} }); +foo("hello", 1, 2); +foo("hello", 1, 2); +foo("hello", "world"); + From 2499b3ebd562846e8986e5ed1aa0247135a9a95e Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 16 Apr 2015 10:21:49 -0700 Subject: [PATCH 3/6] Organize test files and add spec description in the comment --- .../diagnosticInformationMap.generated.ts | 1 - ...structuringParameterDeclaration.errors.txt | 37 --- .../destructuringParameterDeclaration.js | 58 ---- ...tructuringParameterDeclaration1.errors.txt | 271 ++++++++++++--- .../destructuringParameterDeclaration1.js | 309 +++++++++++++----- ...cturingParameterDeclaration1ES6.errors.txt | 48 --- .../destructuringParameterDeclaration1ES6.js | 39 --- ...tructuringParameterDeclaration2.errors.txt | 149 ++++++--- .../destructuringParameterDeclaration2.js | 183 +++++++++-- .../destructuringParameterDeclaration2ES6.js | 42 --- ...ructuringParameterDeclaration3.errors.txt} | 46 ++- .../destructuringParameterDeclaration3.js | 80 +++-- .../destructuringParameterDeclaration3.types | 92 ------ .../destructuringParameterDeclaration3ES6.js | 45 --- ...estructuringParameterDeclaration3ES6.types | 92 ------ ...tructuringParameterDeclaration4.errors.txt | 97 ++++-- .../destructuringParameterDeclaration4.js | 139 +++++--- ...ucturingParameterDeclarationES6.errors.txt | 37 --- .../destructuringParameterDeclarationES6.js | 44 --- .../destructuringParameterDeclaration.ts | 19 -- .../destructuringParameterDeclaration1.ts | 127 +++++-- .../destructuringParameterDeclaration1ES6.ts | 22 -- .../destructuringParameterDeclaration2.ts | 68 +++- .../destructuringParameterDeclaration2ES6.ts | 20 -- .../destructuringParameterDeclaration3.ts | 35 +- .../destructuringParameterDeclaration3ES6.ts | 24 -- .../destructuringParameterDeclaration4.ts | 63 +++- .../destructuringParameterDeclarationES6.ts | 20 -- 28 files changed, 1179 insertions(+), 1028 deletions(-) delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration.errors.txt delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration.js delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.js delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration2ES6.js rename tests/baselines/reference/{destructuringParameterDeclaration2ES6.errors.txt => destructuringParameterDeclaration3.errors.txt} (60%) delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration3.types delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES6.js delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES6.types delete mode 100644 tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt delete mode 100644 tests/baselines/reference/destructuringParameterDeclarationES6.js delete mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts delete mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts delete mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts delete mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts delete mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 0271ac731053e..6e956889b494a 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -1,6 +1,5 @@ // /// -/* @internal */ module ts { export var Diagnostics = { Unterminated_string_literal: { code: 1002, category: DiagnosticCategory.Error, key: "Unterminated string literal." }, diff --git a/tests/baselines/reference/destructuringParameterDeclaration.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration.errors.txt deleted file mode 100644 index 643305a0167f1..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration.errors.txt +++ /dev/null @@ -1,37 +0,0 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(17,1): error TS2346: Supplied parameters do not match any signature of call target. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(18,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. - Property 'z' is missing in type '{}'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(19,14): error TS2300: Duplicate identifier 'z'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts(19,18): error TS2300: Duplicate identifier 'z'. - - -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts (4 errors) ==== - enum Foo { a } - function a({x, a}: { x: number, a: number }) { } - function a1({z: {x, y: {j}}}) { } - function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } - function a3({z} = {z:10}) { } - function a4({z=10}) { } - function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } - a2(); - a2({ z: { x: "hello" , y: { j: Foo.a } }}); - a3(); - a3({ z: Foo.a }); - a4({}); - a6({ b: 10 }); - a6({ b: true }); - - // error - a4(); - ~~~~ -!!! error TS2346: Supplied parameters do not match any signature of call target. - a3({}); - ~~ -!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. -!!! error TS2345: Property 'z' is missing in type '{}'. - function a5([z], z: number) { } - ~ -!!! error TS2300: Duplicate identifier 'z'. - ~ -!!! error TS2300: Duplicate identifier 'z'. - \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration.js b/tests/baselines/reference/destructuringParameterDeclaration.js deleted file mode 100644 index da979430ff320..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration.js +++ /dev/null @@ -1,58 +0,0 @@ -//// [destructuringParameterDeclaration.ts] -enum Foo { a } -function a({x, a}: { x: number, a: number }) { } -function a1({z: {x, y: {j}}}) { } -function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } -function a3({z} = {z:10}) { } -function a4({z=10}) { } -function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } -a2(); -a2({ z: { x: "hello" , y: { j: Foo.a } }}); -a3(); -a3({ z: Foo.a }); -a4({}); -a6({ b: 10 }); -a6({ b: true }); - -// error -a4(); -a3({}); -function a5([z], z: number) { } - - -//// [destructuringParameterDeclaration.js] -var Foo; -(function (Foo) { - Foo[Foo["a"] = 0] = "a"; -})(Foo || (Foo = {})); -function a(_a) { - var x = _a.x, a = _a.a; -} -function a1(_a) { - var _b = _a.z, x = _b.x, j = _b.y.j; -} -function a2(_a) { - var _b = (_a === void 0 ? { z: { x: "hi", y: { j: 1 } } } : _a).z, x = _b.x, j = _b.y.j; -} -function a3(_a) { - var z = (_a === void 0 ? { z: 10 } : _a).z; -} -function a4(_a) { - var _b = _a.z, z = _b === void 0 ? 10 : _b; -} -function a6(_a) { - var b = (_a === void 0 ? { b: "hello" } : _a).b; -} -a2(); -a2({ z: { x: "hello", y: { j: Foo.a } } }); -a3(); -a3({ z: Foo.a }); -a4({}); -a6({ b: 10 }); -a6({ b: true }); -// error -a4(); -a3({}); -function a5(_a, z) { - var z = _a[0]; -} diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt index 2651be05038d2..cc0e8167d1c3e 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt @@ -1,48 +1,233 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(5,16): error TS1048: A rest parameter cannot have an initializer. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(11,13): error TS2370: A rest parameter must be of an array type. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(12,18): error TS1047: A rest parameter cannot be optional. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(13,17): error TS1048: A rest parameter cannot have an initializer. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(16,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. - Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(21,4): error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. - Type 'boolean' is not assignable to type 'string | number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(7,10): error TS2393: Duplicate function implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(10,10): error TS2393: Duplicate function implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(14,4): error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. + Property 'x' is missing in type '(string | number | string[][])[]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(14,29): error TS1005: ',' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(15,4): error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(28,8): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(28,16): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(35,14): error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. + Types of property 'x' are incompatible. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(44,14): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(44,18): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(50,4): error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. + Types of property 'z' are incompatible. + Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(52,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. + Property 'z' is missing in type '{}'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(53,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. + Types of property 'z' are incompatible. Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(59,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. + Types of property 'z' are incompatible. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(62,4): error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. + Types of property 'b' are incompatible. + Type 'boolean' is not assignable to type 'string | number'. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(66,4): error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. + Types of property '2' are incompatible. + Type 'boolean' is not assignable to type '[[any]]'. + Property '0' is missing in type 'Boolean'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(68,4): error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. + Types of property '2' are incompatible. + Type '[[string]]' is not assignable to type '[[number]]'. + Types of property '0' are incompatible. + Type '[string]' is not assignable to type '[number]'. + Types of property '0' are incompatible. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(73,10): error TS2393: Duplicate function implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(74,10): error TS2393: Duplicate function implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(75,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(76,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(97,7): error TS2420: Class 'C4' incorrectly implements interface 'F2'. + Types of property 'd4' are incompatible. + Type '({x, y, c}: { x: any; y: any; c: any; }) => void' is not assignable to type '({x, y, z}?: { x: any; y: any; z: any; }) => any'. + Types of parameters '__0' and '__0' are incompatible. + Type '{ x: any; y: any; c: any; }' is not assignable to type '{ x: any; y: any; z: any; }'. + Property 'z' is missing in type '{ x: any; y: any; c: any; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(98,8): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts (6 errors) ==== - type arrayString = Array - type someArray = Array | number[]; - type stringOrNumArray = Array; - - function a3(...x = [1,2,3]) { } - ~ -!!! error TS1048: A rest parameter cannot have an initializer. - function a4(...x: (number|string)[]) { } - function a5(...a) { } - function a6(...a: Array) { } - function a7(...a: arrayString) { } - function a8(...a: stringOrNumArray) { } - function a9(...a: someArray) { } - ~~~~~~~~~~~~~~~ -!!! error TS2370: A rest parameter must be of an array type. - function a10(...b?) { } +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts (23 errors) ==== + // A parameter declaration may specify either an identifier or a binding pattern. + // The identifiers specified in parameter declarations and binding patterns + // in a parameter list must be unique within that parameter list. + + // If the declaration includes a type annotation, the parameter is of that type + function a0(x: number, y: string, z: boolean) { } + function a1([a, b, [[c]]]: [number, number, string[][]]) { } + ~~ +!!! error TS2393: Duplicate function implementation. + function a2(o: { x: number, a: number }) { } + function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; + function a1({x, a}: { x: number, a: number }) { } + ~~ +!!! error TS2393: Duplicate function implementation. + + a1([1, 2, [["world"]]]); + a1([1, 2, [["world"]], 3]); + a1([1, "string", [["world"]]); // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. +!!! error TS2345: Property 'x' is missing in type '(string | number | string[][])[]'. + ~ +!!! error TS1005: ',' expected. + a1([1, 2, [["world"]], "string"]); // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. + + + // If the declaration includes an initializer expression (which is permitted only + // when the parameter list occurs in conjunction with a function body), + // the parameter type is the widened form (section 3.11) of the type of the initializer expression. + + function b1(z = 10, y = 60, u = () => true) { } + function b2(z = [undefined, null]) { }; + function b3(z = null, o = { x: 0, y: undefined }) { } + function b4({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + + interface F1 { + b5(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body + ~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + b6(z, y, [, a, b], {p, m: { q, r}}); + } + + b2([1, 2, 3]); // z is widen to the type any[] + b3("string", { x: 200, y: "string" }); + b3("string", { x: 200, y: true }); + b3("string", { x: "string", y: true }); // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. +!!! error TS2345: Types of property 'x' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'number'. + + + // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) + enum Foo { a } + function c0({z: {x, y: {j}}}) { } + function c1({z} = { z: 10 }) { } + function c2({z = 10}) { } + function c3({b}: { b: number|string} = { b: "hello" }) { } + function c4([z], z: number) { } // Duplicate identifier + ~ +!!! error TS2300: Duplicate identifier 'z'. ~ -!!! error TS1047: A rest parameter cannot be optional. - function a11(...b = [1,2,3]) { } - ~ -!!! error TS1048: A rest parameter cannot have an initializer. - - - a4(1, 2, "hello", true); - ~~~~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. - var array = [1, 2, 3]; - var array2 = [true, false, "hello"]; - a5([...array]); - a4(...array); - a4(...array2); +!!! error TS2300: Duplicate identifier 'z'. + function c5([a, b, [[c]]]) { } + function c6([a, b, [[c=1]]]) { } + + c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } + c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } + c0({z : 1}); // Error, implied type is { z: {x: any, y: {j: any}} } + ~~~~~~~ +!!! error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. +!!! error TS2345: Types of property 'z' are incompatible. +!!! error TS2345: Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'. + + c1({}); // Error, implied type is {z:number}? + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. +!!! error TS2345: Property 'z' is missing in type '{}'. + c1({ z: true }); // Error, implied type is {z:number}? + ~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. +!!! error TS2345: Types of property 'z' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + c1(); // Implied type is {z:number}? + c1({ z: 1 }) // Implied type is {z:number}? + + c2({}); // Implied type is {z?: number} + c2({z:1}); // Implied type is {z?: number} + c2({z:false}); // Error, implied type is {z?: number} ~~~~~~~~~ -!!! error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. \ No newline at end of file +!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. +!!! error TS2345: Types of property 'z' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + + c3({ b: 1 }); // Implied type is { b: number|string }. + c3({ b: true }); // Error, implied type is { b: number|string }. + ~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. +!!! error TS2345: Types of property 'b' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + + c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] + c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] + c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] + ~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. +!!! error TS2345: Types of property '2' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type '[[any]]'. +!!! error TS2345: Property '0' is missing in type 'Boolean'. + + c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. +!!! error TS2345: Types of property '2' are incompatible. +!!! error TS2345: Type '[[string]]' is not assignable to type '[[number]]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type '[string]' is not assignable to type '[number]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'number'. + + // A parameter can be marked optional by following its name or binding pattern with a question mark (?) + // or by including an initializer. + + function d0(x?) { } + ~~ +!!! error TS2393: Duplicate function implementation. + function d0(x = 10) { } + ~~ +!!! error TS2393: Duplicate function implementation. + function d1([a, b, c]?) { } + ~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + function d2({x, y, z}?) { } + ~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + + interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); + } + + class C2 implements F2 { + constructor() { } + d3() { } + d4() { } + e0([a, b, c]) { } + } + + class C3 implements F2 { + d3([a, b, c]) { } + d4({x, y, z}) { } + e0([a, b, c]) { } + } + + class C4 implements F2 { + ~~ +!!! error TS2420: Class 'C4' incorrectly implements interface 'F2'. +!!! error TS2420: Types of property 'd4' are incompatible. +!!! error TS2420: Type '({x, y, c}: { x: any; y: any; c: any; }) => void' is not assignable to type '({x, y, z}?: { x: any; y: any; z: any; }) => any'. +!!! error TS2420: Types of parameters '__0' and '__0' are incompatible. +!!! error TS2420: Type '{ x: any; y: any; c: any; }' is not assignable to type '{ x: any; y: any; z: any; }'. +!!! error TS2420: Property 'z' is missing in type '{ x: any; y: any; c: any; }'. + d3([a, b, c]?) { } + ~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + d4({x, y, c}) { } + e0([a, b, q]) { } + } + + function d5({x, y} = { x: 1, y: 2 }) { } + d5(); // Parameter is optional as its declaration included an initializer + + + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.js b/tests/baselines/reference/destructuringParameterDeclaration1.js index 37967ddb4415f..d896351183852 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration1.js +++ b/tests/baselines/reference/destructuringParameterDeclaration1.js @@ -1,86 +1,249 @@ //// [destructuringParameterDeclaration1.ts] -type arrayString = Array -type someArray = Array | number[]; -type stringOrNumArray = Array; - -function a3(...x = [1,2,3]) { } -function a4(...x: (number|string)[]) { } -function a5(...a) { } -function a6(...a: Array) { } -function a7(...a: arrayString) { } -function a8(...a: stringOrNumArray) { } -function a9(...a: someArray) { } -function a10(...b?) { } -function a11(...b = [1,2,3]) { } - - -a4(1, 2, "hello", true); -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a5([...array]); -a4(...array); -a4(...array2); +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a0(x: number, y: string, z: boolean) { } +function a1([a, b, [[c]]]: [number, number, string[][]]) { } +function a2(o: { x: number, a: number }) { } +function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; +function a1({x, a}: { x: number, a: number }) { } + +a1([1, 2, [["world"]]]); +a1([1, 2, [["world"]], 3]); +a1([1, "string", [["world"]]); // Error +a1([1, 2, [["world"]], "string"]); // Error + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +function b1(z = 10, y = 60, u = () => true) { } +function b2(z = [undefined, null]) { }; +function b3(z = null, o = { x: 0, y: undefined }) { } +function b4({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + +interface F1 { + b5(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body + b6(z, y, [, a, b], {p, m: { q, r}}); +} + +b2([1, 2, 3]); // z is widen to the type any[] +b3("string", { x: 200, y: "string" }); +b3("string", { x: 200, y: true }); +b3("string", { x: "string", y: true }); // Error + + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +enum Foo { a } +function c0({z: {x, y: {j}}}) { } +function c1({z} = { z: 10 }) { } +function c2({z = 10}) { } +function c3({b}: { b: number|string} = { b: "hello" }) { } +function c4([z], z: number) { } // Duplicate identifier +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c=1]]]) { } + +c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } +c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } +c0({z : 1}); // Error, implied type is { z: {x: any, y: {j: any}} } + +c1({}); // Error, implied type is {z:number}? +c1({ z: true }); // Error, implied type is {z:number}? +c1(); // Implied type is {z:number}? +c1({ z: 1 }) // Implied type is {z:number}? + +c2({}); // Implied type is {z?: number} +c2({z:1}); // Implied type is {z?: number} +c2({z:false}); // Error, implied type is {z?: number} + +c3({ b: 1 }); // Implied type is { b: number|string }. +c3({ b: true }); // Error, implied type is { b: number|string }. + +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] +c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] + +c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. + +function d0(x?) { } +function d0(x = 10) { } +function d1([a, b, c]?) { } +function d2({x, y, z}?) { } + +interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); +} + +class C2 implements F2 { + constructor() { } + d3() { } + d4() { } + e0([a, b, c]) { } +} + +class C3 implements F2 { + d3([a, b, c]) { } + d4({x, y, z}) { } + e0([a, b, c]) { } +} + +class C4 implements F2 { + d3([a, b, c]?) { } + d4({x, y, c}) { } + e0([a, b, q]) { } +} + +function d5({x, y} = { x: 1, y: 2 }) { } +d5(); // Parameter is optional as its declaration included an initializer + + + //// [destructuringParameterDeclaration1.js] -function a3() { - if (x === void 0) { x = [1, 2, 3]; } - var x = []; - for (var _i = 0; _i < arguments.length; _i++) { - x[_i - 0] = arguments[_i]; - } +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. +// If the declaration includes a type annotation, the parameter is of that type +function a0(x, y, z) { } +function a1(_a) { + var a = _a[0], b = _a[1], c = _a[2][0][0]; } -function a4() { - var x = []; - for (var _i = 0; _i < arguments.length; _i++) { - x[_i - 0] = arguments[_i]; - } +function a2(o) { } +function a3(_a) { + var j = _a.j, k = _a.k, _b = _a.l, m = _b.m, n = _b.n, _c = _a.q, a = _c[0], b = _c[1], c = _c[2]; } -function a5() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +; +function a1(_a) { + var x = _a.x, a = _a.a; } -function a6() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +a1([1, 2, [["world"]]]); +a1([1, 2, [["world"]], 3]); +a1([1, "string", [["world"]]]); // Error +a1([1, 2, [["world"]], "string"]); // Error +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. +function b1(z, y, u) { + if (z === void 0) { z = 10; } + if (y === void 0) { y = 60; } + if (u === void 0) { u = function () { return true; }; } } -function a7() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +function b2(z) { + if (z === void 0) { z = [undefined, null]; } } -function a8() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +; +function b3(z, o) { + if (z === void 0) { z = null; } + if (o === void 0) { o = { x: 0, y: undefined }; } } -function a9() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +function b4(_a) { + var _b = (_a === void 0 ? { z: { x: "hi", y: { j: 1 } } } : _a).z, x = _b.x, j = _b.y.j; } -function a10() { - var b = []; - for (var _i = 0; _i < arguments.length; _i++) { - b[_i - 0] = arguments[_i]; - } +b2([1, 2, 3]); // z is widen to the type any[] +b3("string", { x: 200, y: "string" }); +b3("string", { x: 200, y: true }); +b3("string", { x: "string", y: true }); // Error +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +var Foo; +(function (Foo) { + Foo[Foo["a"] = 0] = "a"; +})(Foo || (Foo = {})); +function c0(_a) { + var _b = _a.z, x = _b.x, j = _b.y.j; } -function a11() { - if (b === void 0) { b = [1, 2, 3]; } - var b = []; - for (var _i = 0; _i < arguments.length; _i++) { - b[_i - 0] = arguments[_i]; +function c1(_a) { + var z = (_a === void 0 ? { z: 10 } : _a).z; +} +function c2(_a) { + var _b = _a.z, z = _b === void 0 ? 10 : _b; +} +function c3(_a) { + var b = (_a === void 0 ? { b: "hello" } : _a).b; +} +function c4(_a, z) { + var z = _a[0]; +} // Duplicate identifier +function c5(_a) { + var a = _a[0], b = _a[1], c = _a[2][0][0]; +} +function c6(_a) { + var a = _a[0], b = _a[1], _b = _a[2][0][0], c = _b === void 0 ? 1 : _b; +} +c0({ z: { x: 1, y: { j: "world" } } }); // Implied type is { z: {x: any, y: {j: any}} } +c0({ z: { x: "string", y: { j: true } } }); // Implied type is { z: {x: any, y: {j: any}} } +c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} } +c1({}); // Error, implied type is {z:number}? +c1({ z: true }); // Error, implied type is {z:number}? +c1(); // Implied type is {z:number}? +c1({ z: 1 }); // Implied type is {z:number}? +c2({}); // Implied type is {z?: number} +c2({ z: 1 }); // Implied type is {z?: number} +c2({ z: false }); // Error, implied type is {z?: number} +c3({ b: 1 }); // Implied type is { b: number|string }. +c3({ b: true }); // Error, implied type is { b: number|string }. +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] +c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] +c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. +function d0(x) { } +function d0(x) { + if (x === void 0) { x = 10; } +} +function d1(_a) { + var a = _a[0], b = _a[1], c = _a[2]; +} +function d2(_a) { + var x = _a.x, y = _a.y, z = _a.z; +} +var C2 = (function () { + function C2() { + } + C2.prototype.d3 = function () { }; + C2.prototype.d4 = function () { }; + C2.prototype.e0 = function (_a) { + var a = _a[0], b = _a[1], c = _a[2]; + }; + return C2; +})(); +var C3 = (function () { + function C3() { + } + C3.prototype.d3 = function (_a) { + var a = _a[0], b = _a[1], c = _a[2]; + }; + C3.prototype.d4 = function (_a) { + var x = _a.x, y = _a.y, z = _a.z; + }; + C3.prototype.e0 = function (_a) { + var a = _a[0], b = _a[1], c = _a[2]; + }; + return C3; +})(); +var C4 = (function () { + function C4() { } + C4.prototype.d3 = function (_a) { + var a = _a[0], b = _a[1], c = _a[2]; + }; + C4.prototype.d4 = function (_a) { + var x = _a.x, y = _a.y, c = _a.c; + }; + C4.prototype.e0 = function (_a) { + var a = _a[0], b = _a[1], q = _a[2]; + }; + return C4; +})(); +function d5(_a) { + var _b = _a === void 0 ? { x: 1, y: 2 } : _a, x = _b.x, y = _b.y; } -a4(1, 2, "hello", true); -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a5(array); -a4.apply(void 0, array); -a4.apply(void 0, array2); +d5(); // Parameter is optional as its declaration included an initializer diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt deleted file mode 100644 index 786571c88a587..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration1ES6.errors.txt +++ /dev/null @@ -1,48 +0,0 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(5,16): error TS1048: A rest parameter cannot have an initializer. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(11,13): error TS2370: A rest parameter must be of an array type. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(12,18): error TS1047: A rest parameter cannot be optional. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(13,17): error TS1048: A rest parameter cannot have an initializer. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(16,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. - Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(21,4): error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. - Type 'boolean' is not assignable to type 'string | number'. - Type 'boolean' is not assignable to type 'number'. - - -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts (6 errors) ==== - type arrayString = Array - type someArray = Array | number[]; - type stringOrNumArray = Array; - - function a3(...x = [1,2,3]) { } - ~ -!!! error TS1048: A rest parameter cannot have an initializer. - function a4(...x: (number|string)[]) { } - function a5(...a) { } - function a6(...a: Array) { } - function a7(...a: arrayString) { } - function a8(...a: stringOrNumArray) { } - function a9(...a: someArray) { } - ~~~~~~~~~~~~~~~ -!!! error TS2370: A rest parameter must be of an array type. - function a10(...b?) { } - ~ -!!! error TS1047: A rest parameter cannot be optional. - function a11(...b = [1,2,3]) { } - ~ -!!! error TS1048: A rest parameter cannot have an initializer. - - - a4(1, 2, "hello", true); - ~~~~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. - var array = [1, 2, 3]; - var array2 = [true, false, "hello"]; - a5([...array]); - a4(...array); - a4(...array2); - ~~~~~~~~~ -!!! error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.js b/tests/baselines/reference/destructuringParameterDeclaration1ES6.js deleted file mode 100644 index 779f5495f5682..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration1ES6.js +++ /dev/null @@ -1,39 +0,0 @@ -//// [destructuringParameterDeclaration1ES6.ts] -type arrayString = Array -type someArray = Array | number[]; -type stringOrNumArray = Array; - -function a3(...x = [1,2,3]) { } -function a4(...x: (number|string)[]) { } -function a5(...a) { } -function a6(...a: Array) { } -function a7(...a: arrayString) { } -function a8(...a: stringOrNumArray) { } -function a9(...a: someArray) { } -function a10(...b?) { } -function a11(...b = [1,2,3]) { } - - -a4(1, 2, "hello", true); -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a5([...array]); -a4(...array); -a4(...array2); - -//// [destructuringParameterDeclaration1ES6.js] -function a3(...x = [1, 2, 3]) { } -function a4(...x) { } -function a5(...a) { } -function a6(...a) { } -function a7(...a) { } -function a8(...a) { } -function a9(...a) { } -function a10(...b) { } -function a11(...b = [1, 2, 3]) { } -a4(1, 2, "hello", true); -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a5([...array]); -a4(...array); -a4(...array2); diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt index afe984cc03194..314c1aafee667 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt @@ -1,56 +1,117 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,14): error TS1181: Array element destructuring pattern expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,19): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,21): error TS1109: Expression expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,24): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,26): error TS2304: Cannot find name 'public'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,32): error TS1005: ';' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(6,33): error TS1128: Declaration or statement expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,16): error TS1003: Identifier expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,21): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(9,13): error TS2370: A rest parameter must be of an array type. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(12,24): error TS1005: ',' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(11,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(17,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(25,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(30,4): error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. + Type 'boolean' is not assignable to type 'string | number'. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(33,4): error TS2345: Argument of type '[number, number, string, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. + Types of property '2' are incompatible. + Type 'string' is not assignable to type '[[any]]'. + Property '0' is missing in type 'String'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(34,4): error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]]]'. + Property '2' is missing in type '[number, number]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(41,5): error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'. + Type 'string | number' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(45,24): error TS1005: ',' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(50,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. + Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(59,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. + Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'. -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts (11 errors) ==== - "use strict" - function a({while}) { } - function a1({public}) { } - function a2({public: x}) { } - function a3({while: y}) { } - function a4([while, for, public]){ } - ~~~~~ -!!! error TS1181: Array element destructuring pattern expected. - ~ -!!! error TS1005: '(' expected. - ~~~ -!!! error TS1109: Expression expected. - ~ -!!! error TS1005: '(' expected. - ~~~~~~ -!!! error TS2304: Cannot find name 'public'. - ~ -!!! error TS1005: ';' expected. - ~ -!!! error TS1128: Declaration or statement expected. - function a5(...while) { } - ~~~~~ -!!! error TS1003: Identifier expected. - ~ -!!! error TS1005: '(' expected. - function a6(...public) { } - function a7(...a: string) { } - ~~~~~~~~~~~~ +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts (10 errors) ==== + // If the parameter is a rest parameter, the parameter type is any[] + // A type annotation for a rest parameter must denote an array type. + + // RestParameter: + // ... Identifier TypeAnnotation(opt) + + type arrayString = Array + type someArray = Array | number[]; + type stringOrNumArray = Array; + + function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2370: A rest parameter must be of an array type. + function a1(...x: (number|string)[]) { } + function a2(...a) { } + function a3(...a: Array) { } + function a4(...a: arrayString) { } + function a5(...a: stringOrNumArray) { } + function a6(...a: someArray) { } // Error, rest parameter must be array type + ~~~~~~~~~~~~~~~ +!!! error TS2370: A rest parameter must be of an array type. + function a7(...b?) { } // Error, can't be optional + function a8(...b = [1,2,3]) { } // Error, can't have initializer + function a9([a, b, [[c]]]) { } + function a10([a, b, [[c]], ...x]) { } + function a11([a, b, c, ...x]: number[]) { } + + + a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + var array = [1, 2, 3]; + var array2 = [true, false, "hello"]; + a2([...array]); + a1(...array); + a1(...array2); // Error parameter type is (number|string)[] + ~~~~~~~~~ +!!! error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + + a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] + a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[number, number, string, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. +!!! error TS2345: Types of property '2' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type '[[any]]'. +!!! error TS2345: Property '0' is missing in type 'String'. + a9([1, 2]); // Error, parameter type is [any, any, [[any]]] + ~~~~~~ +!!! error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]]]'. +!!! error TS2345: Property '2' is missing in type '[number, number]'. + + a10([1, 2, [["string"]], false, true]); // Parameter type is any[] + a10([1, 2, 3, false, true]); // Parameter type is any[] + a10([1, 2]); // Parameter type is any[] + + a11([1, 2]); // Parameter type is number[] + a11([1, 2, "string"]); // Error, parameter type is number[] + ~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'. +!!! error TS2345: Type 'string | number' is not assignable to type 'number'. +!!! error TS2345: Type 'string' is not assignable to type 'number'. + class C { - constructor(public ...a) { } + constructor(public ...a) { } // Rest parameter can't have accessibilityModifier ~~~ !!! error TS1005: ',' expected. } + // Rest parameter with generic + function foo(...a: T[]) { } + foo("hello", 1, 2); // Error + ~~~ +!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. +!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. + foo("hello", 1, 2); + foo("hello", "world"); + + enum E { a, b } + const enum E1 { a, b } + function foo1(...a: T[]) { } + foo1(1, 2, 3, E.a); + foo1(1, 2, 3, E1.a, E.b); + foo1(1, 2, "string", E1.a, E.b); // Error + ~~~~ +!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. +!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'. - a2({ public: 1 }); - a3({ while: 1 }); - a({ while: 1 }); \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.js b/tests/baselines/reference/destructuringParameterDeclaration2.js index ba6d754844215..11bda7049e2bb 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2.js +++ b/tests/baselines/reference/destructuringParameterDeclaration2.js @@ -1,65 +1,178 @@ //// [destructuringParameterDeclaration2.ts] -"use strict" -function a({while}) { } -function a1({public}) { } -function a2({public: x}) { } -function a3({while: y}) { } -function a4([while, for, public]){ } -function a5(...while) { } -function a6(...public) { } -function a7(...a: string) { } +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type +function a1(...x: (number|string)[]) { } +function a2(...a) { } +function a3(...a: Array) { } +function a4(...a: arrayString) { } +function a5(...a: stringOrNumArray) { } +function a6(...a: someArray) { } // Error, rest parameter must be array type +function a7(...b?) { } // Error, can't be optional +function a8(...b = [1,2,3]) { } // Error, can't have initializer +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]: number[]) { } + + +a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); +a1(...array2); // Error parameter type is (number|string)[] + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] +a9([1, 2]); // Error, parameter type is [any, any, [[any]]] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] + +a11([1, 2]); // Parameter type is number[] +a11([1, 2, "string"]); // Error, parameter type is number[] + class C { - constructor(public ...a) { } + constructor(public ...a) { } // Rest parameter can't have accessibilityModifier } +// Rest parameter with generic +function foo(...a: T[]) { } +foo("hello", 1, 2); // Error +foo("hello", 1, 2); +foo("hello", "world"); + +enum E { a, b } +const enum E1 { a, b } +function foo1(...a: T[]) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, "string", E1.a, E.b); // Error -a2({ public: 1 }); -a3({ while: 1 }); -a({ while: 1 }); //// [destructuringParameterDeclaration2.js] -"use strict"; -function a(_a) { - var while = _a.while; +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. +function a0() { + var x = []; + for (var _i = 0; _i < arguments.length; _i++) { + x[_i - 0] = arguments[_i]; + } +} // Error, rest parameter must be array type +function a1() { + var x = []; + for (var _i = 0; _i < arguments.length; _i++) { + x[_i - 0] = arguments[_i]; + } } -function a1(_a) { - var public = _a.public; +function a2() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } } -function a2(_a) { - var x = _a.public; +function a3() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } } -function a3(_a) { - var y = _a.while; +function a4() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } } -while (, ) - for (, public; ; ) - ; -{ } -while () { } -function a6() { - var public = []; +function a5() { + var a = []; for (var _i = 0; _i < arguments.length; _i++) { - public[_i - 0] = arguments[_i]; + a[_i - 0] = arguments[_i]; } } -function a7() { +function a6() { var a = []; for (var _i = 0; _i < arguments.length; _i++) { a[_i - 0] = arguments[_i]; } +} // Error, rest parameter must be array type +function a7() { + var b = []; + for (var _i = 0; _i < arguments.length; _i++) { + b[_i - 0] = arguments[_i]; + } +} // Error, can't be optional +function a8() { + if (b === void 0) { b = [1, 2, 3]; } + var b = []; + for (var _i = 0; _i < arguments.length; _i++) { + b[_i - 0] = arguments[_i]; + } +} // Error, can't have initializer +function a9(_a) { + var a = _a[0], b = _a[1], c = _a[2][0][0]; +} +function a10(_a) { + var a = _a[0], b = _a[1], c = _a[2][0][0], x = _a.slice(3); } +function a11(_a) { + var a = _a[0], b = _a[1], c = _a[2], x = _a.slice(3); +} +a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2(array); +a1.apply(void 0, array); +a1.apply(void 0, array2); // Error parameter type is (number|string)[] +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] +a9([1, 2]); // Error, parameter type is [any, any, [[any]]] +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] +a11([1, 2]); // Parameter type is number[] +a11([1, 2, "string"]); // Error, parameter type is number[] var C = (function () { function C(public) { var a = []; for (var _i = 1; _i < arguments.length; _i++) { a[_i - 1] = arguments[_i]; } - } + } // Rest parameter can't have accessibilityModifier return C; })(); -a2({ public: 1 }); -a3({ while: 1 }); -a({ while: 1 }); +// Rest parameter with generic +function foo() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +foo("hello", 1, 2); // Error +foo("hello", 1, 2); +foo("hello", "world"); +var E; +(function (E) { + E[E["a"] = 0] = "a"; + E[E["b"] = 1] = "b"; +})(E || (E = {})); +function foo1() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } +} +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, 0 /* a */, E.b); +foo1(1, 2, "string", 0 /* a */, E.b); // Error diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.js b/tests/baselines/reference/destructuringParameterDeclaration2ES6.js deleted file mode 100644 index 6e86aa2c001e0..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration2ES6.js +++ /dev/null @@ -1,42 +0,0 @@ -//// [destructuringParameterDeclaration2ES6.ts] -"use strict" -function a({while}) { } -function a1({public}) { } -function a2({public: x}) { } -function a3({while: y}) { } -function a4([while, for, public]){ } -function a5(...while) { } -function a6(...public) { } -function a7(...a: string) { } - -class C { - constructor(public ...a) { } -} - - -a2({ public: 1 }); -a3({ while: 1 }); -a({ while: 1 }); - - - -//// [destructuringParameterDeclaration2ES6.js] -"use strict"; -function a({ while }) { } -function a1({ public }) { } -function a2({ public: x }) { } -function a3({ while: y }) { } -while (, ) - for (, public; ; ) - ; -{ } -while () { } -function a6(...public) { } -function a7(...a) { } -class C { - constructor(public, ...a) { - } -} -a2({ public: 1 }); -a3({ while: 1 }); -a({ while: 1 }); diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration3.errors.txt similarity index 60% rename from tests/baselines/reference/destructuringParameterDeclaration2ES6.errors.txt rename to tests/baselines/reference/destructuringParameterDeclaration3.errors.txt index 762a0e1dd8a18..577775827315c 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2ES6.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration3.errors.txt @@ -1,22 +1,24 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,14): error TS1181: Array element destructuring pattern expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,19): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,21): error TS1109: Expression expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,24): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,26): error TS2304: Cannot find name 'public'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,32): error TS1005: ';' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(6,33): error TS1128: Declaration or statement expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(7,16): error TS1003: Identifier expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(7,21): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(9,13): error TS2370: A rest parameter must be of an array type. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts(12,24): error TS1005: ',' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,14): error TS1181: Array element destructuring pattern expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,19): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,21): error TS1109: Expression expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,24): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,26): error TS2304: Cannot find name 'public'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,32): error TS1005: ';' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,33): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(10,16): error TS1003: Identifier expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(10,21): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(12,13): error TS2370: A rest parameter must be of an array type. -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts (11 errors) ==== +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts (10 errors) ==== + // A parameter declaration may specify either an identifier or a binding pattern. + + // Reserved words are not allowed to be used as an identifier in parameter declaration "use strict" + + // Error function a({while}) { } function a1({public}) { } - function a2({public: x}) { } - function a3({while: y}) { } function a4([while, for, public]){ } ~~~~~ !!! error TS1181: Array element destructuring pattern expected. @@ -41,16 +43,12 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6. function a7(...a: string) { } ~~~~~~~~~~~~ !!! error TS2370: A rest parameter must be of an array type. - - class C { - constructor(public ...a) { } - ~~~ -!!! error TS1005: ',' expected. - } - - - a2({ public: 1 }); - a3({ while: 1 }); a({ while: 1 }); + // No Error + function b1({public: x}) { } + function b2({while: y}) { } + b1({ public: 1 }); + b2({ while: 1 }); + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration3.js b/tests/baselines/reference/destructuringParameterDeclaration3.js index 82d1404881c97..a1abb86bd63e8 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration3.js +++ b/tests/baselines/reference/destructuringParameterDeclaration3.js @@ -1,51 +1,61 @@ //// [destructuringParameterDeclaration3.ts] -function a([x, [y, z], [[j]]]) { - return [x, y, z, j]; -} +// A parameter declaration may specify either an identifier or a binding pattern. -function a1([...x]) { - return [x]; -} +// Reserved words are not allowed to be used as an identifier in parameter declaration +"use strict" -function a2({public} = { "public": "1" }) { - return public; -} +// Error +function a({while}) { } +function a1({public}) { } +function a4([while, for, public]){ } +function a5(...while) { } +function a6(...public) { } +function a7(...a: string) { } +a({ while: 1 }); -function a3({x: { y, z}, j: {k: {a}} }) { - return [y, z, a]; -} +// No Error +function b1({public: x}) { } +function b2({while: y}) { } +b1({ public: 1 }); +b2({ while: 1 }); -function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { - return [y, z, a]; -} - -function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { - return [y, z, a]; -} //// [destructuringParameterDeclaration3.js] +// A parameter declaration may specify either an identifier or a binding pattern. +// Reserved words are not allowed to be used as an identifier in parameter declaration +"use strict"; +// Error function a(_a) { - var x = _a[0], _b = _a[1], y = _b[0], z = _b[1], j = _a[2][0][0]; - return [x, y, z, j]; + var while = _a.while; } function a1(_a) { - var x = _a.slice(0); - return [x]; + var public = _a.public; } -function a2(_a) { - var public = (_a === void 0 ? { "public": "1" } : _a).public; - return public; +while (, ) + for (, public; ; ) + ; +{ } +while () { } +function a6() { + var public = []; + for (var _i = 0; _i < arguments.length; _i++) { + public[_i - 0] = arguments[_i]; + } } -function a3(_a) { - var _b = _a.x, y = _b.y, z = _b.z, a = _a.j.k.a; - return [y, z, a]; +function a7() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } } -function a4(_a) { - var _b = _a === void 0 ? { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } } : _a, _c = _b.x, y = _c.y, z = _c.z, a = _b.j.k.a; - return [y, z, a]; +a({ while: 1 }); +// No Error +function b1(_a) { + var x = _a.public; } -function a5(_a) { - var _b = _a.x, y = _b.y, z = _b.z, a = _a.j.k.a; - return [y, z, a]; +function b2(_a) { + var y = _a.while; } +b1({ public: 1 }); +b2({ while: 1 }); diff --git a/tests/baselines/reference/destructuringParameterDeclaration3.types b/tests/baselines/reference/destructuringParameterDeclaration3.types deleted file mode 100644 index 41835d9e456c5..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration3.types +++ /dev/null @@ -1,92 +0,0 @@ -=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts === -function a([x, [y, z], [[j]]]) { ->a : ([x, [y, z], [[j]]]: [any, [any, any], [[any]]]) => any[] ->x : any ->y : any ->z : any ->j : any - - return [x, y, z, j]; ->[x, y, z, j] : any[] ->x : any ->y : any ->z : any ->j : any -} - -function a1([...x]) { ->a1 : ([...x]: any[]) => any[][] ->x : any[] - - return [x]; ->[x] : any[][] ->x : any[] -} - -function a2({public} = { "public": "1" }) { ->a2 : ({public}?: { "public": string; }) => string ->public : string ->{ "public": "1" } : { "public": string; } - - return public; ->public : string -} - -function a3({x: { y, z}, j: {k: {a}} }) { ->a3 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => any[] ->x : unknown ->y : any ->z : any ->j : unknown ->k : unknown ->a : any - - return [y, z, a]; ->[y, z, a] : any[] ->y : any ->z : any ->a : any -} - -function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { ->a4 : ({x: { y, z}, j: {k: {a}} }?: { x: { y: number; z: number; }; j: { k: { a: string; }; }; }) => (string | number)[] ->x : unknown ->y : number ->z : number ->j : unknown ->k : unknown ->a : string ->{ x: { y: 1, z: 1 }, j: { k: { a: "hello" } } } : { x: { y: number; z: number; }; j: { k: { a: string; }; }; } ->x : { y: number; z: number; } ->{ y: 1, z: 1 } : { y: number; z: number; } ->y : number ->z : number ->j : { k: { a: string; }; } ->{ k: { a: "hello" } } : { k: { a: string; }; } ->k : { a: string; } ->{ a: "hello" } : { a: string; } ->a : string - - return [y, z, a]; ->[y, z, a] : (string | number)[] ->y : number ->z : number ->a : string -} - -function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { ->a5 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => (string | number)[] ->x : unknown ->y : any ->z : any ->j : unknown ->k : unknown ->a : any - - return [y, z, a]; ->[y, z, a] : any[] ->y : any ->z : any ->a : any -} - diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES6.js b/tests/baselines/reference/destructuringParameterDeclaration3ES6.js deleted file mode 100644 index e129140f3f29f..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration3ES6.js +++ /dev/null @@ -1,45 +0,0 @@ -//// [destructuringParameterDeclaration3ES6.ts] -function a([x, [y, z], [[j]]]) { - return [x, y, z, j]; -} - -function a1([...x]) { - return [x]; -} - -function a2({public} = { "public": "1" }) { - return public; -} - -function a3({x: { y, z}, j: {k: {a}} }) { - return [y, z, a]; -} - -function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { - return [y, z, a]; -} - -function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { - return [y, z, a]; -} - - -//// [destructuringParameterDeclaration3ES6.js] -function a([x, [y, z], [[j]]]) { - return [x, y, z, j]; -} -function a1([...x]) { - return [x]; -} -function a2({ public } = { "public": "1" }) { - return public; -} -function a3({ x: { y, z }, j: { k: { a } } }) { - return [y, z, a]; -} -function a4({ x: { y, z }, j: { k: { a } } } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }) { - return [y, z, a]; -} -function a5({ x: { y, z }, j: { k: { a } } }) { - return [y, z, a]; -} diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES6.types b/tests/baselines/reference/destructuringParameterDeclaration3ES6.types deleted file mode 100644 index 01c67e1e547ca..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration3ES6.types +++ /dev/null @@ -1,92 +0,0 @@ -=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts === -function a([x, [y, z], [[j]]]) { ->a : ([x, [y, z], [[j]]]: [any, [any, any], [[any]]]) => any[] ->x : any ->y : any ->z : any ->j : any - - return [x, y, z, j]; ->[x, y, z, j] : any[] ->x : any ->y : any ->z : any ->j : any -} - -function a1([...x]) { ->a1 : ([...x]: Iterable) => any[][] ->x : any[] - - return [x]; ->[x] : any[][] ->x : any[] -} - -function a2({public} = { "public": "1" }) { ->a2 : ({public}?: { "public": string; }) => string ->public : string ->{ "public": "1" } : { "public": string; } - - return public; ->public : string -} - -function a3({x: { y, z}, j: {k: {a}} }) { ->a3 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => any[] ->x : unknown ->y : any ->z : any ->j : unknown ->k : unknown ->a : any - - return [y, z, a]; ->[y, z, a] : any[] ->y : any ->z : any ->a : any -} - -function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { ->a4 : ({x: { y, z}, j: {k: {a}} }?: { x: { y: number; z: number; }; j: { k: { a: string; }; }; }) => (string | number)[] ->x : unknown ->y : number ->z : number ->j : unknown ->k : unknown ->a : string ->{ x: { y: 1, z: 1 }, j: { k: { a: "hello" } } } : { x: { y: number; z: number; }; j: { k: { a: string; }; }; } ->x : { y: number; z: number; } ->{ y: 1, z: 1 } : { y: number; z: number; } ->y : number ->z : number ->j : { k: { a: string; }; } ->{ k: { a: "hello" } } : { k: { a: string; }; } ->k : { a: string; } ->{ a: "hello" } : { a: string; } ->a : string - - return [y, z, a]; ->[y, z, a] : (string | number)[] ->y : number ->z : number ->a : string -} - -function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { ->a5 : ({x: { y, z}, j: {k: {a}} }: { x: { y: any; z: any; }; j: { k: { a: any; }; }; }) => (string | number)[] ->x : unknown ->y : any ->z : any ->j : unknown ->k : unknown ->a : any - - return [y, z, a]; ->[y, z, a] : any[] ->y : any ->z : any ->a : any -} - diff --git a/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt index cf4b15d12714b..d5d0888e632cb 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt @@ -1,27 +1,78 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(15,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. - Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(46,4): error TS2345: Argument of type '{ y: Class; }' is not assignable to parameter of type '{ y: D; }'. + Types of property 'y' are incompatible. + Type 'Class' is not assignable to type 'D'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(47,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ y: D; }'. + Property 'y' is missing in type '{}'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(48,4): error TS2345: Argument of type '{ y: number; }' is not assignable to parameter of type '{ y: D; }'. + Types of property 'y' are incompatible. + Type 'number' is not assignable to type 'D'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(49,4): error TS2345: Argument of type '{ y: string; }' is not assignable to parameter of type '{ y: D; }'. + Types of property 'y' are incompatible. + Type 'string' is not assignable to type 'D'. -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts (1 errors) ==== +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts (4 errors) ==== + // Parameter with generic interface F { } - class C implements F{ + class Class implements F { + constructor() { } } - function foo(...a: T[]) { } - function foo1(...a: T[]) { } - function bar({x} = { x: new C() }) { } - function baz({x}: { x: F }) { } - function baz1({x}: { x: C }) { } - function baz2({x}: { x: C }) { } - - var obj = new C(); - baz1({ x: obj }); - baz({ x: new C() }); - baz({ x: {} }); - foo("hello", 1, 2); - ~~~ -!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. -!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. - foo("hello", 1, 2); - foo("hello", "world"); - - \ No newline at end of file + + class SubClass extends Class { + foo: boolean; + constructor() { super(); } + } + + class D implements F { + foo: boolean + constructor() { } + } + + class SubD extends D { + bar: number + constructor() { + super(); + } + } + + + function d0({x} = { x: new Class() }) { } + function d1({x}: { x: F }) { } + function d2({x}: { x: Class }) { } + function d3({y}: { y: D }) { } + function d4({y} = { y: new D() }) { } + + var obj = new Class(); + d0({ x: 1 }); + d0({ x: {} }); + d0({ x: "string" }); + + d1({ x: new Class() }); + d1({ x: {} }); + d1({ x: "string" }); + + d2({ x: new SubClass() }); + d2({ x: {} }); + + d3({ y: new SubD() }); + d3({ y: new SubClass() }); + // Error + d3({ y: new Class() }); + ~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ y: Class; }' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Types of property 'y' are incompatible. +!!! error TS2345: Type 'Class' is not assignable to type 'D'. + d3({}); + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Property 'y' is missing in type '{}'. + d3({ y: 1 }); + ~~~~~~~~ +!!! error TS2345: Argument of type '{ y: number; }' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Types of property 'y' are incompatible. +!!! error TS2345: Type 'number' is not assignable to type 'D'. + d3({ y: "world" }); + ~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ y: string; }' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Types of property 'y' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'D'. \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration4.js b/tests/baselines/reference/destructuringParameterDeclaration4.js index 3f83402dffabc..3eb0b3f8c1930 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration4.js +++ b/tests/baselines/reference/destructuringParameterDeclaration4.js @@ -1,58 +1,113 @@ //// [destructuringParameterDeclaration4.ts] +// Parameter with generic interface F { } -class C implements F{ +class Class implements F { + constructor() { } } -function foo(...a: T[]) { } -function foo1(...a: T[]) { } -function bar({x} = { x: new C() }) { } -function baz({x}: { x: F }) { } -function baz1({x}: { x: C }) { } -function baz2({x}: { x: C }) { } - -var obj = new C(); -baz1({ x: obj }); -baz({ x: new C() }); -baz({ x: {} }); -foo("hello", 1, 2); -foo("hello", 1, 2); -foo("hello", "world"); - +class SubClass extends Class { + foo: boolean; + constructor() { super(); } +} + +class D implements F { + foo: boolean + constructor() { } +} + +class SubD extends D { + bar: number + constructor() { + super(); + } +} + + +function d0({x} = { x: new Class() }) { } +function d1({x}: { x: F }) { } +function d2({x}: { x: Class }) { } +function d3({y}: { y: D }) { } +function d4({y} = { y: new D() }) { } + +var obj = new Class(); +d0({ x: 1 }); +d0({ x: {} }); +d0({ x: "string" }); + +d1({ x: new Class() }); +d1({ x: {} }); +d1({ x: "string" }); + +d2({ x: new SubClass() }); +d2({ x: {} }); + +d3({ y: new SubD() }); +d3({ y: new SubClass() }); +// Error +d3({ y: new Class() }); +d3({}); +d3({ y: 1 }); +d3({ y: "world" }); //// [destructuringParameterDeclaration4.js] -var C = (function () { - function C() { +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +var Class = (function () { + function Class() { } - return C; + return Class; })(); -function foo() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; +var SubClass = (function (_super) { + __extends(SubClass, _super); + function SubClass() { + _super.call(this); } -} -function foo1() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; + return SubClass; +})(Class); +var D = (function () { + function D() { } + return D; +})(); +var SubD = (function (_super) { + __extends(SubD, _super); + function SubD() { + _super.call(this); + } + return SubD; +})(D); +function d0(_a) { + var x = (_a === void 0 ? { x: new Class() } : _a).x; } -function bar(_a) { - var x = (_a === void 0 ? { x: new C() } : _a).x; -} -function baz(_a) { +function d1(_a) { var x = _a.x; } -function baz1(_a) { +function d2(_a) { var x = _a.x; } -function baz2(_a) { - var x = _a.x; +function d3(_a) { + var y = _a.y; +} +function d4(_a) { + var y = (_a === void 0 ? { y: new D() } : _a).y; } -var obj = new C(); -baz1({ x: obj }); -baz({ x: new C() }); -baz({ x: {} }); -foo("hello", 1, 2); -foo("hello", 1, 2); -foo("hello", "world"); +var obj = new Class(); +d0({ x: 1 }); +d0({ x: {} }); +d0({ x: "string" }); +d1({ x: new Class() }); +d1({ x: {} }); +d1({ x: "string" }); +d2({ x: new SubClass() }); +d2({ x: {} }); +d3({ y: new SubD() }); +d3({ y: new SubClass() }); +// Error +d3({ y: new Class() }); +d3({}); +d3({ y: 1 }); +d3({ y: "world" }); diff --git a/tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt b/tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt deleted file mode 100644 index 21cd9f36c0fdb..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclarationES6.errors.txt +++ /dev/null @@ -1,37 +0,0 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(17,1): error TS2346: Supplied parameters do not match any signature of call target. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(18,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. - Property 'z' is missing in type '{}'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(19,14): error TS2300: Duplicate identifier 'z'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts(19,18): error TS2300: Duplicate identifier 'z'. - - -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts (4 errors) ==== - enum Foo { a } - function a({x, a}: { x: number, a: number }) { } - function a1({z: {x, y: {j}}}) { } - function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } - function a3({z} = {z:10}) { } - function a4({z=10}) { } - function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } - a2(); - a2({ z: { x: "hello" , y: { j: Foo.a } }}); - a3(); - a3({ z: Foo.a }); - a4({}); - a6({ b: 10 }); - a6({ b: true }); - - // error - a4(); - ~~~~ -!!! error TS2346: Supplied parameters do not match any signature of call target. - a3({}); - ~~ -!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. -!!! error TS2345: Property 'z' is missing in type '{}'. - function a5([z], z: number) { } - ~ -!!! error TS2300: Duplicate identifier 'z'. - ~ -!!! error TS2300: Duplicate identifier 'z'. - \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclarationES6.js b/tests/baselines/reference/destructuringParameterDeclarationES6.js deleted file mode 100644 index 7a4c929003d60..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclarationES6.js +++ /dev/null @@ -1,44 +0,0 @@ -//// [destructuringParameterDeclarationES6.ts] -enum Foo { a } -function a({x, a}: { x: number, a: number }) { } -function a1({z: {x, y: {j}}}) { } -function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } -function a3({z} = {z:10}) { } -function a4({z=10}) { } -function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } -a2(); -a2({ z: { x: "hello" , y: { j: Foo.a } }}); -a3(); -a3({ z: Foo.a }); -a4({}); -a6({ b: 10 }); -a6({ b: true }); - -// error -a4(); -a3({}); -function a5([z], z: number) { } - - -//// [destructuringParameterDeclarationES6.js] -var Foo; -(function (Foo) { - Foo[Foo["a"] = 0] = "a"; -})(Foo || (Foo = {})); -function a({ x, a }) { } -function a1({ z: { x, y: { j } } }) { } -function a2({ z: { x, y: { j } } } = { z: { x: "hi", y: { j: 1 } } }) { } -function a3({ z } = { z: 10 }) { } -function a4({ z = 10 }) { } -function a6({ b } = { b: "hello" }) { } -a2(); -a2({ z: { x: "hello", y: { j: Foo.a } } }); -a3(); -a3({ z: Foo.a }); -a4({}); -a6({ b: 10 }); -a6({ b: true }); -// error -a4(); -a3({}); -function a5([z], z) { } diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts deleted file mode 100644 index c840dba2d2b62..0000000000000 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration.ts +++ /dev/null @@ -1,19 +0,0 @@ -enum Foo { a } -function a({x, a}: { x: number, a: number }) { } -function a1({z: {x, y: {j}}}) { } -function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } -function a3({z} = {z:10}) { } -function a4({z=10}) { } -function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } -a2(); -a2({ z: { x: "hello" , y: { j: Foo.a } }}); -a3(); -a3({ z: Foo.a }); -a4({}); -a6({ b: 10 }); -a6({ b: true }); - -// error -a4(); -a3({}); -function a5([z], z: number) { } diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts index 6e9579926dd44..027d11b8c2f47 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts @@ -1,21 +1,106 @@ -type arrayString = Array -type someArray = Array | number[]; -type stringOrNumArray = Array; - -function a3(...x = [1,2,3]) { } -function a4(...x: (number|string)[]) { } -function a5(...a) { } -function a6(...a: Array) { } -function a7(...a: arrayString) { } -function a8(...a: stringOrNumArray) { } -function a9(...a: someArray) { } -function a10(...b?) { } -function a11(...b = [1,2,3]) { } - - -a4(1, 2, "hello", true); -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a5([...array]); -a4(...array); -a4(...array2); \ No newline at end of file +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a0(x: number, y: string, z: boolean) { } +function a1([a, b, [[c]]]: [number, number, string[][]]) { } +function a2(o: { x: number, a: number }) { } +function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; +function a1({x, a}: { x: number, a: number }) { } + +a1([1, 2, [["world"]]]); +a1([1, 2, [["world"]], 3]); +a1([1, "string", [["world"]]); // Error +a1([1, 2, [["world"]], "string"]); // Error + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +function b1(z = 10, y = 60, u = () => true) { } +function b2(z = [undefined, null]) { }; +function b3(z = null, o = { x: 0, y: undefined }) { } +function b4({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + +interface F1 { + b5(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body + b6(z, y, [, a, b], {p, m: { q, r}}); +} + +b2([1, 2, 3]); // z is widen to the type any[] +b3("string", { x: 200, y: "string" }); +b3("string", { x: 200, y: true }); +b3("string", { x: "string", y: true }); // Error + + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +enum Foo { a } +function c0({z: {x, y: {j}}}) { } +function c1({z} = { z: 10 }) { } +function c2({z = 10}) { } +function c3({b}: { b: number|string} = { b: "hello" }) { } +function c4([z], z: number) { } // Duplicate identifier +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c=1]]]) { } + +c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } +c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } +c0({z : 1}); // Error, implied type is { z: {x: any, y: {j: any}} } + +c1({}); // Error, implied type is {z:number}? +c1({ z: true }); // Error, implied type is {z:number}? +c1(); // Implied type is {z:number}? +c1({ z: 1 }) // Implied type is {z:number}? + +c2({}); // Implied type is {z?: number} +c2({z:1}); // Implied type is {z?: number} +c2({z:false}); // Error, implied type is {z?: number} + +c3({ b: 1 }); // Implied type is { b: number|string }. +c3({ b: true }); // Error, implied type is { b: number|string }. + +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] +c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] + +c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. + +function d0(x?) { } +function d0(x = 10) { } +function d1([a, b, c]?) { } +function d2({x, y, z}?) { } + +interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); +} + +class C2 implements F2 { + constructor() { } + d3() { } + d4() { } + e0([a, b, c]) { } +} + +class C3 implements F2 { + d3([a, b, c]) { } + d4({x, y, z}) { } + e0([a, b, c]) { } +} + +class C4 implements F2 { + d3([a, b, c]?) { } + d4({x, y, c}) { } + e0([a, b, q]) { } +} + +function d5({x, y} = { x: 1, y: 2 }) { } +d5(); // Parameter is optional as its declaration included an initializer + + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts deleted file mode 100644 index 1441226e061f6..0000000000000 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts +++ /dev/null @@ -1,22 +0,0 @@ -// @target: es6 -type arrayString = Array -type someArray = Array | number[]; -type stringOrNumArray = Array; - -function a3(...x = [1,2,3]) { } -function a4(...x: (number|string)[]) { } -function a5(...a) { } -function a6(...a: Array) { } -function a7(...a: arrayString) { } -function a8(...a: stringOrNumArray) { } -function a9(...a: someArray) { } -function a10(...b?) { } -function a11(...b = [1,2,3]) { } - - -a4(1, 2, "hello", true); -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a5([...array]); -a4(...array); -a4(...array2); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts index 59763c959027a..5a775eb69c9a0 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts @@ -1,19 +1,61 @@ -"use strict" -function a({while}) { } -function a1({public}) { } -function a2({public: x}) { } -function a3({while: y}) { } -function a4([while, for, public]){ } -function a5(...while) { } -function a6(...public) { } -function a7(...a: string) { } +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type +function a1(...x: (number|string)[]) { } +function a2(...a) { } +function a3(...a: Array) { } +function a4(...a: arrayString) { } +function a5(...a: stringOrNumArray) { } +function a6(...a: someArray) { } // Error, rest parameter must be array type +function a7(...b?) { } // Error, can't be optional +function a8(...b = [1,2,3]) { } // Error, can't have initializer +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]: number[]) { } + + +a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); +a1(...array2); // Error parameter type is (number|string)[] + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] +a9([1, 2]); // Error, parameter type is [any, any, [[any]]] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] + +a11([1, 2]); // Parameter type is number[] +a11([1, 2, "string"]); // Error, parameter type is number[] + class C { - constructor(public ...a) { } + constructor(public ...a) { } // Rest parameter can't have accessibilityModifier } +// Rest parameter with generic +function foo(...a: T[]) { } +foo("hello", 1, 2); // Error +foo("hello", 1, 2); +foo("hello", "world"); + +enum E { a, b } +const enum E1 { a, b } +function foo1(...a: T[]) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, "string", E1.a, E.b); // Error -a2({ public: 1 }); -a3({ while: 1 }); -a({ while: 1 }); diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts deleted file mode 100644 index e50a455d8241d..0000000000000 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts +++ /dev/null @@ -1,20 +0,0 @@ -//@target: es6 -"use strict" -function a({while}) { } -function a1({public}) { } -function a2({public: x}) { } -function a3({while: y}) { } -function a4([while, for, public]){ } -function a5(...while) { } -function a6(...public) { } -function a7(...a: string) { } - -class C { - constructor(public ...a) { } -} - - -a2({ public: 1 }); -a3({ while: 1 }); -a({ while: 1 }); - diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts index 96fc87c6c86ac..c583b750fef5a 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts @@ -1,23 +1,20 @@ -function a([x, [y, z], [[j]]]) { - return [x, y, z, j]; -} +// A parameter declaration may specify either an identifier or a binding pattern. -function a1([...x]) { - return [x]; -} +// Reserved words are not allowed to be used as an identifier in parameter declaration +"use strict" -function a2({public} = { "public": "1" }) { - return public; -} +// Error +function a({while}) { } +function a1({public}) { } +function a4([while, for, public]){ } +function a5(...while) { } +function a6(...public) { } +function a7(...a: string) { } +a({ while: 1 }); -function a3({x: { y, z}, j: {k: {a}} }) { - return [y, z, a]; -} +// No Error +function b1({public: x}) { } +function b2({while: y}) { } +b1({ public: 1 }); +b2({ while: 1 }); -function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { - return [y, z, a]; -} - -function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { - return [y, z, a]; -} diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts deleted file mode 100644 index 7c50917ebe5f7..0000000000000 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts +++ /dev/null @@ -1,24 +0,0 @@ -// @target: es6 -function a([x, [y, z], [[j]]]) { - return [x, y, z, j]; -} - -function a1([...x]) { - return [x]; -} - -function a2({public} = { "public": "1" }) { - return public; -} - -function a3({x: { y, z}, j: {k: {a}} }) { - return [y, z, a]; -} - -function a4({x: { y, z}, j: {k: {a}} } = { x: { y: 1, z: 1 }, j: { k: { a: "hello" } } }): (number| string) [] { - return [y, z, a]; -} - -function a5({x: { y, z}, j: {k: {a}} }): (number| string) [] { - return [y, z, a]; -} diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts index b59c539d672b5..4f0f52d30656a 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts @@ -1,18 +1,49 @@ -interface F { } -class C implements F{ +// Parameter with generic +interface F { } +class Class implements F { + constructor() { } } -function foo(...a: T[]) { } -function foo1(...a: T[]) { } -function bar({x} = { x: new C() }) { } -function baz({x}: { x: F }) { } -function baz1({x}: { x: C }) { } -function baz2({x}: { x: C }) { } - -var obj = new C(); -baz1({ x: obj }); -baz({ x: new C() }); -baz({ x: {} }); -foo("hello", 1, 2); -foo("hello", 1, 2); -foo("hello", "world"); +class SubClass extends Class { + foo: boolean; + constructor() { super(); } +} + +class D implements F { + foo: boolean + constructor() { } +} + +class SubD extends D { + bar: number + constructor() { + super(); + } +} + + +function d0({x} = { x: new Class() }) { } +function d1({x}: { x: F }) { } +function d2({x}: { x: Class }) { } +function d3({y}: { y: D }) { } +function d4({y} = { y: new D() }) { } + +var obj = new Class(); +d0({ x: 1 }); +d0({ x: {} }); +d0({ x: "string" }); + +d1({ x: new Class() }); +d1({ x: {} }); +d1({ x: "string" }); + +d2({ x: new SubClass() }); +d2({ x: {} }); + +d3({ y: new SubD() }); +d3({ y: new SubClass() }); +// Error +d3({ y: new Class() }); +d3({}); +d3({ y: 1 }); +d3({ y: "world" }); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts deleted file mode 100644 index 0d3594218517d..0000000000000 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclarationES6.ts +++ /dev/null @@ -1,20 +0,0 @@ -//@target: es6 -enum Foo { a } -function a({x, a}: { x: number, a: number }) { } -function a1({z: {x, y: {j}}}) { } -function a2({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } -function a3({z} = {z:10}) { } -function a4({z=10}) { } -function a6({b}: { b: number|string|boolean } = { b: "hello" }) { } -a2(); -a2({ z: { x: "hello" , y: { j: Foo.a } }}); -a3(); -a3({ z: Foo.a }); -a4({}); -a6({ b: 10 }); -a6({ b: true }); - -// error -a4(); -a3({}); -function a5([z], z: number) { } From 614469462ad6931b0d540aad6e8d7bd1d74b9b31 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 16 Apr 2015 13:35:56 -0700 Subject: [PATCH 4/6] Update tests files with more conformance from section 6.4 --- ...tructuringParameterDeclaration1.errors.txt | 129 +++--- .../destructuringParameterDeclaration1.js | 98 +++-- .../destructuringParameterDeclaration1ES6.js | 169 ++++++++ ...estructuringParameterDeclaration1ES6.types | 378 ++++++++++++++++++ ...tructuringParameterDeclaration2.errors.txt | 2 +- .../destructuringParameterDeclaration2.js | 4 +- .../destructuringParameterDeclaration2ES6.js | 81 ++++ ...estructuringParameterDeclaration2ES6.types | 170 ++++++++ .../destructuringParameterDeclaration1.ts | 43 +- .../destructuringParameterDeclaration1ES6.ts | 99 +++++ .../destructuringParameterDeclaration2.ts | 2 +- .../destructuringParameterDeclaration2ES6.ts | 47 +++ 12 files changed, 1129 insertions(+), 93 deletions(-) create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.types create mode 100644 tests/baselines/reference/destructuringParameterDeclaration2ES6.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration2ES6.types create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt index cc0e8167d1c3e..078895d07b061 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt @@ -1,53 +1,65 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(7,10): error TS2393: Duplicate function implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(10,10): error TS2393: Duplicate function implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(14,4): error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. - Property 'x' is missing in type '(string | number | string[][])[]'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(14,29): error TS1005: ',' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(15,4): error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(28,8): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(28,16): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(35,14): error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(13,4): error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'. + Types of property '1' are incompatible. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(13,29): error TS1005: ',' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(14,4): error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'. + Types of property 'pop' are incompatible. + Type '() => string | number | string[][]' is not assignable to type '() => number | string[][]'. + Type 'string | number | string[][]' is not assignable to type 'number | string[][]'. + Type 'string' is not assignable to type 'number | string[][]'. + Type 'string' is not assignable to type 'string[][]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(26,8): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(26,16): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(36,14): error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. Types of property 'x' are incompatible. Type 'string' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(44,14): error TS2300: Duplicate identifier 'z'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(44,18): error TS2300: Duplicate identifier 'z'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(50,4): error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(37,4): error TS2345: Argument of type '[string, number, number]' is not assignable to parameter of type '[undefined, null, undefined]'. + Types of property '0' are incompatible. + Type 'string' is not assignable to type 'undefined'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(38,4): error TS2345: Argument of type '[[string], number, [[boolean, boolean]]]' is not assignable to parameter of type '[[undefined], undefined, [[undefined, undefined]]]'. + Types of property '0' are incompatible. + Type '[string]' is not assignable to type '[undefined]'. + Types of property '0' are incompatible. + Type 'string' is not assignable to type 'undefined'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(47,14): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(47,18): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(53,4): error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. Types of property 'z' are incompatible. Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(52,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(55,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. Property 'z' is missing in type '{}'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(53,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(56,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. Types of property 'z' are incompatible. Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(59,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(62,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. Types of property 'z' are incompatible. Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(62,4): error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(65,4): error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. Types of property 'b' are incompatible. Type 'boolean' is not assignable to type 'string | number'. Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(66,4): error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(69,4): error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. Types of property '2' are incompatible. Type 'boolean' is not assignable to type '[[any]]'. Property '0' is missing in type 'Boolean'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(68,4): error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(71,4): error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. Types of property '2' are incompatible. Type '[[string]]' is not assignable to type '[[number]]'. Types of property '0' are incompatible. Type '[string]' is not assignable to type '[number]'. Types of property '0' are incompatible. Type 'string' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(73,10): error TS2393: Duplicate function implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(74,10): error TS2393: Duplicate function implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(75,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(76,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(97,7): error TS2420: Class 'C4' incorrectly implements interface 'F2'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(76,10): error TS2393: Duplicate function implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(77,10): error TS2393: Duplicate function implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(78,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(79,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(100,7): error TS2420: Class 'C4' incorrectly implements interface 'F2'. Types of property 'd4' are incompatible. Type '({x, y, c}: { x: any; y: any; c: any; }) => void' is not assignable to type '({x, y, z}?: { x: any; y: any; z: any; }) => any'. Types of parameters '__0' and '__0' are incompatible. Type '{ x: any; y: any; c: any; }' is not assignable to type '{ x: any; y: any; z: any; }'. Property 'z' is missing in type '{ x: any; y: any; c: any; }'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(98,8): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(101,8): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. ==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts (23 errors) ==== @@ -56,55 +68,70 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts( // in a parameter list must be unique within that parameter list. // If the declaration includes a type annotation, the parameter is of that type - function a0(x: number, y: string, z: boolean) { } function a1([a, b, [[c]]]: [number, number, string[][]]) { } - ~~ -!!! error TS2393: Duplicate function implementation. function a2(o: { x: number, a: number }) { } function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; - function a1({x, a}: { x: number, a: number }) { } - ~~ -!!! error TS2393: Duplicate function implementation. + function a4({x, a}: { x: number, a: number }) { } a1([1, 2, [["world"]]]); a1([1, 2, [["world"]], 3]); a1([1, "string", [["world"]]); // Error ~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. -!!! error TS2345: Property 'x' is missing in type '(string | number | string[][])[]'. +!!! error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'. +!!! error TS2345: Types of property '1' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'number'. ~ !!! error TS1005: ',' expected. a1([1, 2, [["world"]], "string"]); // Error ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '(string | number | string[][])[]' is not assignable to parameter of type '{ x: number; a: number; }'. +!!! error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'. +!!! error TS2345: Types of property 'pop' are incompatible. +!!! error TS2345: Type '() => string | number | string[][]' is not assignable to type '() => number | string[][]'. +!!! error TS2345: Type 'string | number | string[][]' is not assignable to type 'number | string[][]'. +!!! error TS2345: Type 'string' is not assignable to type 'number | string[][]'. +!!! error TS2345: Type 'string' is not assignable to type 'string[][]'. // If the declaration includes an initializer expression (which is permitted only // when the parameter list occurs in conjunction with a function body), // the parameter type is the widened form (section 3.11) of the type of the initializer expression. - function b1(z = 10, y = 60, u = () => true) { } - function b2(z = [undefined, null]) { }; - function b3(z = null, o = { x: 0, y: undefined }) { } - function b4({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + function b1(z = [undefined, null]) { }; + function b2(z = null, o = { x: 0, y: undefined }) { } + function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } interface F1 { - b5(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body + b4(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body ~~~~~~ !!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - b6(z, y, [, a, b], {p, m: { q, r}}); + b5(z, y, [, a, b], {p, m: { q, r}}); } - b2([1, 2, 3]); // z is widen to the type any[] - b3("string", { x: 200, y: "string" }); - b3("string", { x: 200, y: true }); - b3("string", { x: "string", y: true }); // Error + function b6([a, z, y] = [undefined, null, undefined]) { } + function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + + b1([1, 2, 3]); // z is widen to the type any[] + b2("string", { x: 200, y: "string" }); + b2("string", { x: 200, y: true }); + b2("string", { x: "string", y: true }); // Error ~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. !!! error TS2345: Types of property 'x' are incompatible. !!! error TS2345: Type 'string' is not assignable to type 'number'. + b6(["string", 1, 2]); // Shouldn't be an error + ~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[string, number, number]' is not assignable to parameter of type '[undefined, null, undefined]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'undefined'. + b7([["string"], 1, [[true, false]]]); // Shouldn't be an error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[[string], number, [[boolean, boolean]]]' is not assignable to parameter of type '[[undefined], undefined, [[undefined, undefined]]]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type '[string]' is not assignable to type '[undefined]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'undefined'. // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) @@ -185,10 +212,10 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts( function d0(x = 10) { } ~~ !!! error TS2393: Duplicate function implementation. - function d1([a, b, c]?) { } + function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature ~~~~~~~~~~ !!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. - function d2({x, y, z}?) { } + function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature ~~~~~~~~~~ !!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. @@ -229,5 +256,17 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts( function d5({x, y} = { x: 1, y: 2 }) { } d5(); // Parameter is optional as its declaration included an initializer + // Destructuring parameter declarations do not permit type annotations on the individual binding patterns, + // as such annotations would conflict with the already established meaning of colons in object literals. + // Type annotations must instead be written on the top- level parameter declaration + + function e1({x: number}) { } // x has type any NOT number + function e2({x}: { x: number }) { } // x is type number + function e3({x}: { x?: number }) { } // x is an optional with type number + function e4({x: [number,string,any] }) { } // x has type [any, any, any] + function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] + + function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.js b/tests/baselines/reference/destructuringParameterDeclaration1.js index d896351183852..0d8590ae50a32 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration1.js +++ b/tests/baselines/reference/destructuringParameterDeclaration1.js @@ -4,11 +4,10 @@ // in a parameter list must be unique within that parameter list. // If the declaration includes a type annotation, the parameter is of that type -function a0(x: number, y: string, z: boolean) { } function a1([a, b, [[c]]]: [number, number, string[][]]) { } function a2(o: { x: number, a: number }) { } function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; -function a1({x, a}: { x: number, a: number }) { } +function a4({x, a}: { x: number, a: number }) { } a1([1, 2, [["world"]]]); a1([1, 2, [["world"]], 3]); @@ -20,20 +19,24 @@ a1([1, 2, [["world"]], "string"]); // Error // when the parameter list occurs in conjunction with a function body), // the parameter type is the widened form (section 3.11) of the type of the initializer expression. -function b1(z = 10, y = 60, u = () => true) { } -function b2(z = [undefined, null]) { }; -function b3(z = null, o = { x: 0, y: undefined }) { } -function b4({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +function b1(z = [undefined, null]) { }; +function b2(z = null, o = { x: 0, y: undefined }) { } +function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } interface F1 { - b5(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body - b6(z, y, [, a, b], {p, m: { q, r}}); + b4(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body + b5(z, y, [, a, b], {p, m: { q, r}}); } -b2([1, 2, 3]); // z is widen to the type any[] -b3("string", { x: 200, y: "string" }); -b3("string", { x: 200, y: true }); -b3("string", { x: "string", y: true }); // Error +function b6([a, z, y] = [undefined, null, undefined]) { } +function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + +b1([1, 2, 3]); // z is widen to the type any[] +b2("string", { x: 200, y: "string" }); +b2("string", { x: 200, y: true }); +b2("string", { x: "string", y: true }); // Error +b6(["string", 1, 2]); // Shouldn't be an error +b7([["string"], 1, [[true, false]]]); // Shouldn't be an error // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) @@ -73,8 +76,8 @@ c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // function d0(x?) { } function d0(x = 10) { } -function d1([a, b, c]?) { } -function d2({x, y, z}?) { } +function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature +function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature interface F2 { d3([a, b, c]?); @@ -104,6 +107,18 @@ class C4 implements F2 { function d5({x, y} = { x: 1, y: 2 }) { } d5(); // Parameter is optional as its declaration included an initializer +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e1({x: number}) { } // x has type any NOT number +function e2({x}: { x: number }) { } // x is type number +function e3({x}: { x?: number }) { } // x is an optional with type number +function e4({x: [number,string,any] }) { } // x has type [any, any, any] +function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] + +function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; + @@ -112,7 +127,6 @@ d5(); // Parameter is optional as its declaration included an initializer // The identifiers specified in parameter declarations and binding patterns // in a parameter list must be unique within that parameter list. // If the declaration includes a type annotation, the parameter is of that type -function a0(x, y, z) { } function a1(_a) { var a = _a[0], b = _a[1], c = _a[2][0][0]; } @@ -121,7 +135,7 @@ function a3(_a) { var j = _a.j, k = _a.k, _b = _a.l, m = _b.m, n = _b.n, _c = _a.q, a = _c[0], b = _c[1], c = _c[2]; } ; -function a1(_a) { +function a4(_a) { var x = _a.x, a = _a.a; } a1([1, 2, [["world"]]]); @@ -131,26 +145,29 @@ a1([1, 2, [["world"]], "string"]); // Error // If the declaration includes an initializer expression (which is permitted only // when the parameter list occurs in conjunction with a function body), // the parameter type is the widened form (section 3.11) of the type of the initializer expression. -function b1(z, y, u) { - if (z === void 0) { z = 10; } - if (y === void 0) { y = 60; } - if (u === void 0) { u = function () { return true; }; } -} -function b2(z) { +function b1(z) { if (z === void 0) { z = [undefined, null]; } } ; -function b3(z, o) { +function b2(z, o) { if (z === void 0) { z = null; } if (o === void 0) { o = { x: 0, y: undefined }; } } -function b4(_a) { +function b3(_a) { var _b = (_a === void 0 ? { z: { x: "hi", y: { j: 1 } } } : _a).z, x = _b.x, j = _b.y.j; } -b2([1, 2, 3]); // z is widen to the type any[] -b3("string", { x: 200, y: "string" }); -b3("string", { x: 200, y: true }); -b3("string", { x: "string", y: true }); // Error +function b6(_a) { + var _b = _a === void 0 ? [undefined, null, undefined] : _a, a = _b[0], z = _b[1], y = _b[2]; +} +function b7(_a) { + var _b = _a === void 0 ? [[undefined], undefined, [[undefined, undefined]]] : _a, a = _b[0][0], b = _b[1], _c = _b[2][0], c = _c[0], d = _c[1]; +} +b1([1, 2, 3]); // z is widen to the type any[] +b2("string", { x: 200, y: "string" }); +b2("string", { x: 200, y: true }); +b2("string", { x: "string", y: true }); // Error +b6(["string", 1, 2]); // Shouldn't be an error +b7([["string"], 1, [[true, false]]]); // Shouldn't be an error // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) var Foo; (function (Foo) { @@ -201,10 +218,10 @@ function d0(x) { } function d1(_a) { var a = _a[0], b = _a[1], c = _a[2]; -} +} // Error, binding pattern can't be optional in implementation signature function d2(_a) { var x = _a.x, y = _a.y, z = _a.z; -} +} // Error, binding pattern can't be optional in implementation signature var C2 = (function () { function C2() { } @@ -247,3 +264,24 @@ function d5(_a) { var _b = _a === void 0 ? { x: 1, y: 2 } : _a, x = _b.x, y = _b.y; } d5(); // Parameter is optional as its declaration included an initializer +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration +function e1(_a) { + var number = _a.x; +} // x has type any NOT number +function e2(_a) { + var x = _a.x; +} // x is type number +function e3(_a) { + var x = _a.x; +} // x is an optional with type number +function e4(_a) { + var _b = _a.x, number = _b[0], string = _b[1], any = _b[2]; +} // x has type [any, any, any] +function e5(_a) { + var _b = _a.x, a = _b[0], b = _b[1], c = _b[2]; +} // x has type [any, any, any] +function e6(_a) { + var _b = _a.x, number = _b[0], number = _b[1], number = _b[2]; +} // should be an error, duplicate identifier; diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.js b/tests/baselines/reference/destructuringParameterDeclaration1ES6.js new file mode 100644 index 0000000000000..6310a8f638e2f --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES6.js @@ -0,0 +1,169 @@ +//// [destructuringParameterDeclaration1ES6.ts] +// Conformance for emitting ES6 + +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a1([a, b, [[c]]]: [number, number, string[][]]) { } +function a2(o: { x: number, a: number }) { } +function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; +function a4({x, a}: { x: number, a: number }) { } + +a1([1, 2, [["world"]]]); +a1([1, 2, [["world"]], 3]); + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +function b1(z = [undefined, null]) { }; +function b2(z = null, o = { x: 0, y: undefined }) { } +function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + +interface F1 { + b5(z, y, [, a, b], {p, m: { q, r}}); +} + +function b6([a, z, y] = [undefined, null, undefined]) { } +function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + +b1([1, 2, 3]); // z is widen to the type any[] +b2("string", { x: 200, y: "string" }); +b2("string", { x: 200, y: true }); + + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +enum Foo { a } +function c0({z: {x, y: {j}}}) { } +function c1({z} = { z: 10 }) { } +function c2({z = 10}) { } +function c3({b}: { b: number|string} = { b: "hello" }) { } +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c=1]]]) { } + +c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } +c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } + +c1(); // Implied type is {z:number}? +c1({ z: 1 }) // Implied type is {z:number}? + +c2({}); // Implied type is {z?: number} +c2({z:1}); // Implied type is {z?: number} + +c3({ b: 1 }); // Implied type is { b: number|string }. + +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] + + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. + +interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); +} + +class C2 implements F2 { + constructor() { } + d3() { } + d4() { } + e0([a, b, c]) { } +} + +class C3 implements F2 { + d3([a, b, c]) { } + d4({x, y, z}) { } + e0([a, b, c]) { } +} + +function d5({x, y} = { x: 1, y: 2 }) { } +d5(); // Parameter is optional as its declaration included an initializer + +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e1({x: number}) { } // x has type any NOT number +function e2({x}: { x: number }) { } // x is type number +function e3({x}: { x?: number }) { } // x is an optional with type number +function e4({x: [number,string,any] }) { } // x has type [any, any, any] +function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] + +function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; + + + + +//// [destructuringParameterDeclaration1ES6.js] +// Conformance for emitting ES6 +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. +// If the declaration includes a type annotation, the parameter is of that type +function a1([a, b, [[c]]]) { } +function a2(o) { } +function a3({ j, k, l: { m, n }, q: [a, b, c] }) { } +; +function a4({ x, a }) { } +a1([1, 2, [["world"]]]); +a1([1, 2, [["world"]], 3]); +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. +function b1(z = [undefined, null]) { } +; +function b2(z = null, o = { x: 0, y: undefined }) { } +function b3({ z: { x, y: { j } } } = { z: { x: "hi", y: { j: 1 } } }) { } +function b6([a, z, y] = [undefined, null, undefined]) { } +function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } +b1([1, 2, 3]); // z is widen to the type any[] +b2("string", { x: 200, y: "string" }); +b2("string", { x: 200, y: true }); +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +var Foo; +(function (Foo) { + Foo[Foo["a"] = 0] = "a"; +})(Foo || (Foo = {})); +function c0({ z: { x, y: { j } } }) { } +function c1({ z } = { z: 10 }) { } +function c2({ z = 10 }) { } +function c3({ b } = { b: "hello" }) { } +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c = 1]]]) { } +c0({ z: { x: 1, y: { j: "world" } } }); // Implied type is { z: {x: any, y: {j: any}} } +c0({ z: { x: "string", y: { j: true } } }); // Implied type is { z: {x: any, y: {j: any}} } +c1(); // Implied type is {z:number}? +c1({ z: 1 }); // Implied type is {z:number}? +c2({}); // Implied type is {z?: number} +c2({ z: 1 }); // Implied type is {z?: number} +c3({ b: 1 }); // Implied type is { b: number|string }. +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] +class C2 { + constructor() { + } + d3() { } + d4() { } + e0([a, b, c]) { } +} +class C3 { + d3([a, b, c]) { } + d4({ x, y, z }) { } + e0([a, b, c]) { } +} +function d5({ x, y } = { x: 1, y: 2 }) { } +d5(); // Parameter is optional as its declaration included an initializer +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration +function e1({ x: number }) { } // x has type any NOT number +function e2({ x }) { } // x is type number +function e3({ x }) { } // x is an optional with type number +function e4({ x: [number, string, any] }) { } // x has type [any, any, any] +function e5({ x: [a, b, c] }) { } // x has type [any, any, any] +function e6({ x: [number, number, number] }) { } // should be an error, duplicate identifier; diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.types b/tests/baselines/reference/destructuringParameterDeclaration1ES6.types new file mode 100644 index 0000000000000..ed77f2dc92b32 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES6.types @@ -0,0 +1,378 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts === +// Conformance for emitting ES6 + +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a1([a, b, [[c]]]: [number, number, string[][]]) { } +>a1 : ([a, b, [[c]]]: [number, number, string[][]]) => void +>a : number +>b : number +>c : string + +function a2(o: { x: number, a: number }) { } +>a2 : (o: { x: number; a: number; }) => void +>o : { x: number; a: number; } +>x : number +>a : number + +function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; +>a3 : ({j, k, l: {m, n}, q: [a, b, c]}: { j: number; k: string; l: { m: boolean; n: number; }; q: (string | number)[]; }) => void +>j : number +>k : string +>l : unknown +>m : boolean +>n : number +>q : unknown +>a : string | number +>b : string | number +>c : string | number +>j : number +>k : string +>l : { m: boolean; n: number; } +>m : boolean +>n : number +>q : (string | number)[] + +function a4({x, a}: { x: number, a: number }) { } +>a4 : ({x, a}: { x: number; a: number; }) => void +>x : number +>a : number +>x : number +>a : number + +a1([1, 2, [["world"]]]); +>a1([1, 2, [["world"]]]) : void +>a1 : ([a, b, [[c]]]: [number, number, string[][]]) => void +>[1, 2, [["world"]]] : [number, number, string[][]] +>[["world"]] : string[][] +>["world"] : string[] + +a1([1, 2, [["world"]], 3]); +>a1([1, 2, [["world"]], 3]) : void +>a1 : ([a, b, [[c]]]: [number, number, string[][]]) => void +>[1, 2, [["world"]], 3] : [number, number, string[][], number] +>[["world"]] : string[][] +>["world"] : string[] + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +function b1(z = [undefined, null]) { }; +>b1 : (z?: any[]) => void +>z : any[] +>[undefined, null] : null[] +>undefined : undefined + +function b2(z = null, o = { x: 0, y: undefined }) { } +>b2 : (z?: any, o?: { x: number; y: any; }) => void +>z : any +>o : { x: number; y: any; } +>{ x: 0, y: undefined } : { x: number; y: undefined; } +>x : number +>y : undefined +>undefined : undefined + +function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +>b3 : ({z: {x, y: {j}}}?: { z: { x: string; y: { j: number; }; }; }) => void +>z : unknown +>x : string +>y : unknown +>j : number +>{ z: { x: "hi", y: { j: 1 } } } : { z: { x: string; y: { j: number; }; }; } +>z : { x: string; y: { j: number; }; } +>{ x: "hi", y: { j: 1 } } : { x: string; y: { j: number; }; } +>x : string +>y : { j: number; } +>{ j: 1 } : { j: number; } +>j : number + +interface F1 { +>F1 : F1 + + b5(z, y, [, a, b], {p, m: { q, r}}); +>b5 : (z: any, y: any, [, a, b]: [any, any, any], {p, m: { q, r}}: { p: any; m: { q: any; r: any; }; }) => any +>z : any +>y : any +>a : any +>b : any +>p : any +>m : unknown +>q : any +>r : any +} + +function b6([a, z, y] = [undefined, null, undefined]) { } +>b6 : ([a, z, y]?: [undefined, null, undefined]) => void +>a : any +>z : any +>y : any +>[undefined, null, undefined] : [undefined, null, undefined] +>undefined : undefined +>undefined : undefined + +function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } +>b7 : ([[a], b, [[c, d]]]?: [[undefined], undefined, [[undefined, undefined]]]) => void +>a : any +>b : any +>c : any +>d : any +>[[undefined], undefined, [[undefined, undefined]]] : [[undefined], undefined, [[undefined, undefined]]] +>[undefined] : [undefined] +>undefined : undefined +>undefined : undefined +>[[undefined, undefined]] : [[undefined, undefined]] +>[undefined, undefined] : [undefined, undefined] +>undefined : undefined +>undefined : undefined + +b1([1, 2, 3]); // z is widen to the type any[] +>b1([1, 2, 3]) : void +>b1 : (z?: any[]) => void +>[1, 2, 3] : number[] + +b2("string", { x: 200, y: "string" }); +>b2("string", { x: 200, y: "string" }) : void +>b2 : (z?: any, o?: { x: number; y: any; }) => void +>{ x: 200, y: "string" } : { x: number; y: string; } +>x : number +>y : string + +b2("string", { x: 200, y: true }); +>b2("string", { x: 200, y: true }) : void +>b2 : (z?: any, o?: { x: number; y: any; }) => void +>{ x: 200, y: true } : { x: number; y: boolean; } +>x : number +>y : boolean + + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +enum Foo { a } +>Foo : Foo +>a : Foo + +function c0({z: {x, y: {j}}}) { } +>c0 : ({z: {x, y: {j}}}: { z: { x: any; y: { j: any; }; }; }) => void +>z : unknown +>x : any +>y : unknown +>j : any + +function c1({z} = { z: 10 }) { } +>c1 : ({z}?: { z: number; }) => void +>z : number +>{ z: 10 } : { z: number; } +>z : number + +function c2({z = 10}) { } +>c2 : ({z = 10}: { z?: number; }) => void +>z : number + +function c3({b}: { b: number|string} = { b: "hello" }) { } +>c3 : ({b}?: { b: string | number; }) => void +>b : string | number +>b : string | number +>{ b: "hello" } : { b: string; } +>b : string + +function c5([a, b, [[c]]]) { } +>c5 : ([a, b, [[c]]]: [any, any, [[any]]]) => void +>a : any +>b : any +>c : any + +function c6([a, b, [[c=1]]]) { } +>c6 : ([a, b, [[c=1]]]: [any, any, [[number]]]) => void +>a : any +>b : any +>c : number + +c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } +>c0({z : { x: 1, y: { j: "world" } }}) : void +>c0 : ({z: {x, y: {j}}}: { z: { x: any; y: { j: any; }; }; }) => void +>{z : { x: 1, y: { j: "world" } }} : { z: { x: number; y: { j: string; }; }; } +>z : { x: number; y: { j: string; }; } +>{ x: 1, y: { j: "world" } } : { x: number; y: { j: string; }; } +>x : number +>y : { j: string; } +>{ j: "world" } : { j: string; } +>j : string + +c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } +>c0({z : { x: "string", y: { j: true } }}) : void +>c0 : ({z: {x, y: {j}}}: { z: { x: any; y: { j: any; }; }; }) => void +>{z : { x: "string", y: { j: true } }} : { z: { x: string; y: { j: boolean; }; }; } +>z : { x: string; y: { j: boolean; }; } +>{ x: "string", y: { j: true } } : { x: string; y: { j: boolean; }; } +>x : string +>y : { j: boolean; } +>{ j: true } : { j: boolean; } +>j : boolean + +c1(); // Implied type is {z:number}? +>c1() : void +>c1 : ({z}?: { z: number; }) => void + +c1({ z: 1 }) // Implied type is {z:number}? +>c1({ z: 1 }) : void +>c1 : ({z}?: { z: number; }) => void +>{ z: 1 } : { z: number; } +>z : number + +c2({}); // Implied type is {z?: number} +>c2({}) : void +>c2 : ({z = 10}: { z?: number; }) => void +>{} : {} + +c2({z:1}); // Implied type is {z?: number} +>c2({z:1}) : void +>c2 : ({z = 10}: { z?: number; }) => void +>{z:1} : { z: number; } +>z : number + +c3({ b: 1 }); // Implied type is { b: number|string }. +>c3({ b: 1 }) : void +>c3 : ({b}?: { b: string | number; }) => void +>{ b: 1 } : { b: number; } +>b : number + +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +>c5([1, 2, [["string"]]]) : void +>c5 : ([a, b, [[c]]]: [any, any, [[any]]]) => void +>[1, 2, [["string"]]] : [number, number, [[string]]] +>[["string"]] : [[string]] +>["string"] : [string] + +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] +>c5([1, 2, [["string"]], false, true]) : void +>c5 : ([a, b, [[c]]]: [any, any, [[any]]]) => void +>[1, 2, [["string"]], false, true] : [number, number, [[string]], boolean, boolean] +>[["string"]] : [[string]] +>["string"] : [string] + + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. + +interface F2 { +>F2 : F2 + + d3([a, b, c]?); +>d3 : ([a, b, c]?: [any, any, any]) => any +>a : any +>b : any +>c : any + + d4({x, y, z}?); +>d4 : ({x, y, z}?: { x: any; y: any; z: any; }) => any +>x : any +>y : any +>z : any + + e0([a, b, c]); +>e0 : ([a, b, c]: [any, any, any]) => any +>a : any +>b : any +>c : any +} + +class C2 implements F2 { +>C2 : C2 +>F2 : F2 + + constructor() { } + d3() { } +>d3 : () => void + + d4() { } +>d4 : () => void + + e0([a, b, c]) { } +>e0 : ([a, b, c]: [any, any, any]) => void +>a : any +>b : any +>c : any +} + +class C3 implements F2 { +>C3 : C3 +>F2 : F2 + + d3([a, b, c]) { } +>d3 : ([a, b, c]: [any, any, any]) => void +>a : any +>b : any +>c : any + + d4({x, y, z}) { } +>d4 : ({x, y, z}: { x: any; y: any; z: any; }) => void +>x : any +>y : any +>z : any + + e0([a, b, c]) { } +>e0 : ([a, b, c]: [any, any, any]) => void +>a : any +>b : any +>c : any +} + +function d5({x, y} = { x: 1, y: 2 }) { } +>d5 : ({x, y}?: { x: number; y: number; }) => void +>x : number +>y : number +>{ x: 1, y: 2 } : { x: number; y: number; } +>x : number +>y : number + +d5(); // Parameter is optional as its declaration included an initializer +>d5() : void +>d5 : ({x, y}?: { x: number; y: number; }) => void + +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e1({x: number}) { } // x has type any NOT number +>e1 : ({x: number}: { x: any; }) => void +>x : unknown +>number : any + +function e2({x}: { x: number }) { } // x is type number +>e2 : ({x}: { x: number; }) => void +>x : number +>x : number + +function e3({x}: { x?: number }) { } // x is an optional with type number +>e3 : ({x}: { x?: number; }) => void +>x : number +>x : number + +function e4({x: [number,string,any] }) { } // x has type [any, any, any] +>e4 : ({x: [number,string,any] }: { x: [any, any, any]; }) => void +>x : unknown +>number : any +>string : any +>any : any + +function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] +>e5 : ({x: [a, b, c]}: { x: [number, number, number]; }) => void +>x : unknown +>a : number +>b : number +>c : number +>x : [number, number, number] + +function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; +>e6 : ({x: [number, number, number]}: { x: [any, any, any]; }) => void +>x : unknown +>number : any +>number : any +>number : any + + + diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt index 314c1aafee667..60f8560455901 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt @@ -89,7 +89,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts( class C { - constructor(public ...a) { } // Rest parameter can't have accessibilityModifier + constructor(public ...a) { } // Error, rest parameter can't have accessibilityModifier ~~~ !!! error TS1005: ',' expected. } diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.js b/tests/baselines/reference/destructuringParameterDeclaration2.js index 11bda7049e2bb..187966c35970b 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2.js +++ b/tests/baselines/reference/destructuringParameterDeclaration2.js @@ -43,7 +43,7 @@ a11([1, 2, "string"]); // Error, parameter type is number[] class C { - constructor(public ...a) { } // Rest parameter can't have accessibilityModifier + constructor(public ...a) { } // Error, rest parameter can't have accessibilityModifier } // Rest parameter with generic @@ -149,7 +149,7 @@ var C = (function () { for (var _i = 1; _i < arguments.length; _i++) { a[_i - 1] = arguments[_i]; } - } // Rest parameter can't have accessibilityModifier + } // Error, rest parameter can't have accessibilityModifier return C; })(); // Rest parameter with generic diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.js b/tests/baselines/reference/destructuringParameterDeclaration2ES6.js new file mode 100644 index 0000000000000..903384c7b4216 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration2ES6.js @@ -0,0 +1,81 @@ +//// [destructuringParameterDeclaration2ES6.ts] + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a1(...x: (number|string)[]) { } +function a2(...a) { } +function a3(...a: Array) { } +function a4(...a: arrayString) { } +function a5(...a: stringOrNumArray) { } +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]: number[]) { } + + +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] + +a11([1, 2]); // Parameter type is number[] + +// Rest parameter with generic +function foo(...a: T[]) { } +foo("hello", 1, 2); +foo("hello", "world"); + +enum E { a, b } +const enum E1 { a, b } +function foo1(...a: T[]) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, E1.a, E.b); + + + + +//// [destructuringParameterDeclaration2ES6.js] +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. +function a1(...x) { } +function a2(...a) { } +function a3(...a) { } +function a4(...a) { } +function a5(...a) { } +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]) { } +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] +a11([1, 2]); // Parameter type is number[] +// Rest parameter with generic +function foo(...a) { } +foo("hello", 1, 2); +foo("hello", "world"); +var E; +(function (E) { + E[E["a"] = 0] = "a"; + E[E["b"] = 1] = "b"; +})(E || (E = {})); +function foo1(...a) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, 0 /* a */, E.b); diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.types b/tests/baselines/reference/destructuringParameterDeclaration2ES6.types new file mode 100644 index 0000000000000..6215728fefb32 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration2ES6.types @@ -0,0 +1,170 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts === + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +>arrayString : String[] +>Array : T[] +>String : String + +type someArray = Array | number[]; +>someArray : number[] | String[] +>Array : T[] +>String : String + +type stringOrNumArray = Array; +>stringOrNumArray : (String | Number)[] +>Array : T[] +>String : String +>Number : Number + +function a1(...x: (number|string)[]) { } +>a1 : (...x: (string | number)[]) => void +>x : (string | number)[] + +function a2(...a) { } +>a2 : (...a: any[]) => void +>a : any[] + +function a3(...a: Array) { } +>a3 : (...a: String[]) => void +>a : String[] +>Array : T[] +>String : String + +function a4(...a: arrayString) { } +>a4 : (...a: String[]) => void +>a : String[] +>arrayString : String[] + +function a5(...a: stringOrNumArray) { } +>a5 : (...a: (String | Number)[]) => void +>a : (String | Number)[] +>stringOrNumArray : (String | Number)[] + +function a9([a, b, [[c]]]) { } +>a9 : ([a, b, [[c]]]: [any, any, [[any]]]) => void +>a : any +>b : any +>c : any + +function a10([a, b, [[c]], ...x]) { } +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>a : any +>b : any +>c : any +>x : any[] + +function a11([a, b, c, ...x]: number[]) { } +>a11 : ([a, b, c, ...x]: number[]) => void +>a : number +>b : number +>c : number +>x : number[] + + +var array = [1, 2, 3]; +>array : number[] +>[1, 2, 3] : number[] + +var array2 = [true, false, "hello"]; +>array2 : (string | boolean)[] +>[true, false, "hello"] : (string | boolean)[] + +a2([...array]); +>a2([...array]) : void +>a2 : (...a: any[]) => void +>[...array] : number[] +>...array : number +>array : number[] + +a1(...array); +>a1(...array) : void +>a1 : (...x: (string | number)[]) => void +>...array : number +>array : number[] + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +>a9([1, 2, [["string"]], false, true]) : void +>a9 : ([a, b, [[c]]]: [any, any, [[any]]]) => void +>[1, 2, [["string"]], false, true] : [number, number, [[string]], boolean, boolean] +>[["string"]] : [[string]] +>["string"] : [string] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +>a10([1, 2, [["string"]], false, true]) : void +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>[1, 2, [["string"]], false, true] : (number | boolean | string[][])[] +>[["string"]] : string[][] +>["string"] : string[] + +a10([1, 2, 3, false, true]); // Parameter type is any[] +>a10([1, 2, 3, false, true]) : void +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>[1, 2, 3, false, true] : (number | boolean)[] + +a10([1, 2]); // Parameter type is any[] +>a10([1, 2]) : void +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>[1, 2] : number[] + +a11([1, 2]); // Parameter type is number[] +>a11([1, 2]) : void +>a11 : ([a, b, c, ...x]: number[]) => void +>[1, 2] : number[] + +// Rest parameter with generic +function foo(...a: T[]) { } +>foo : (...a: T[]) => void +>T : T +>a : T[] +>T : T + +foo("hello", 1, 2); +>foo("hello", 1, 2) : void +>foo : (...a: T[]) => void + +foo("hello", "world"); +>foo("hello", "world") : void +>foo : (...a: T[]) => void + +enum E { a, b } +>E : E +>a : E +>b : E + +const enum E1 { a, b } +>E1 : E1 +>a : E1 +>b : E1 + +function foo1(...a: T[]) { } +>foo1 : (...a: T[]) => void +>T : T +>Number : Number +>a : T[] +>T : T + +foo1(1, 2, 3, E.a); +>foo1(1, 2, 3, E.a) : void +>foo1 : (...a: T[]) => void +>E.a : E +>E : typeof E +>a : E + +foo1(1, 2, 3, E1.a, E.b); +>foo1(1, 2, 3, E1.a, E.b) : void +>foo1 : (...a: T[]) => void +>E1.a : E1 +>E1 : typeof E1 +>a : E1 +>E.b : E +>E : typeof E +>b : E + + + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts index 027d11b8c2f47..a36e679751c4b 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts @@ -3,11 +3,10 @@ // in a parameter list must be unique within that parameter list. // If the declaration includes a type annotation, the parameter is of that type -function a0(x: number, y: string, z: boolean) { } function a1([a, b, [[c]]]: [number, number, string[][]]) { } function a2(o: { x: number, a: number }) { } function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; -function a1({x, a}: { x: number, a: number }) { } +function a4({x, a}: { x: number, a: number }) { } a1([1, 2, [["world"]]]); a1([1, 2, [["world"]], 3]); @@ -19,20 +18,24 @@ a1([1, 2, [["world"]], "string"]); // Error // when the parameter list occurs in conjunction with a function body), // the parameter type is the widened form (section 3.11) of the type of the initializer expression. -function b1(z = 10, y = 60, u = () => true) { } -function b2(z = [undefined, null]) { }; -function b3(z = null, o = { x: 0, y: undefined }) { } -function b4({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +function b1(z = [undefined, null]) { }; +function b2(z = null, o = { x: 0, y: undefined }) { } +function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } interface F1 { - b5(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body - b6(z, y, [, a, b], {p, m: { q, r}}); + b4(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body + b5(z, y, [, a, b], {p, m: { q, r}}); } -b2([1, 2, 3]); // z is widen to the type any[] -b3("string", { x: 200, y: "string" }); -b3("string", { x: 200, y: true }); -b3("string", { x: "string", y: true }); // Error +function b6([a, z, y] = [undefined, null, undefined]) { } +function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + +b1([1, 2, 3]); // z is widen to the type any[] +b2("string", { x: 200, y: "string" }); +b2("string", { x: 200, y: true }); +b2("string", { x: "string", y: true }); // Error +b6(["string", 1, 2]); // Shouldn't be an error +b7([["string"], 1, [[true, false]]]); // Shouldn't be an error // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) @@ -72,8 +75,8 @@ c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // function d0(x?) { } function d0(x = 10) { } -function d1([a, b, c]?) { } -function d2({x, y, z}?) { } +function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature +function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature interface F2 { d3([a, b, c]?); @@ -103,4 +106,16 @@ class C4 implements F2 { function d5({x, y} = { x: 1, y: 2 }) { } d5(); // Parameter is optional as its declaration included an initializer +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e1({x: number}) { } // x has type any NOT number +function e2({x}: { x: number }) { } // x is type number +function e3({x}: { x?: number }) { } // x is an optional with type number +function e4({x: [number,string,any] }) { } // x has type [any, any, any] +function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] + +function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts new file mode 100644 index 0000000000000..58d69729b985a --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts @@ -0,0 +1,99 @@ +// Conformance for emitting ES6 +// @target: es6 + +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a1([a, b, [[c]]]: [number, number, string[][]]) { } +function a2(o: { x: number, a: number }) { } +function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; +function a4({x, a}: { x: number, a: number }) { } + +a1([1, 2, [["world"]]]); +a1([1, 2, [["world"]], 3]); + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +function b1(z = [undefined, null]) { }; +function b2(z = null, o = { x: 0, y: undefined }) { } +function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + +interface F1 { + b5(z, y, [, a, b], {p, m: { q, r}}); +} + +function b6([a, z, y] = [undefined, null, undefined]) { } +function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + +b1([1, 2, 3]); // z is widen to the type any[] +b2("string", { x: 200, y: "string" }); +b2("string", { x: 200, y: true }); + + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +enum Foo { a } +function c0({z: {x, y: {j}}}) { } +function c1({z} = { z: 10 }) { } +function c2({z = 10}) { } +function c3({b}: { b: number|string} = { b: "hello" }) { } +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c=1]]]) { } + +c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } +c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } + +c1(); // Implied type is {z:number}? +c1({ z: 1 }) // Implied type is {z:number}? + +c2({}); // Implied type is {z?: number} +c2({z:1}); // Implied type is {z?: number} + +c3({ b: 1 }); // Implied type is { b: number|string }. + +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] + + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. + +interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); +} + +class C2 implements F2 { + constructor() { } + d3() { } + d4() { } + e0([a, b, c]) { } +} + +class C3 implements F2 { + d3([a, b, c]) { } + d4({x, y, z}) { } + e0([a, b, c]) { } +} + +function d5({x, y} = { x: 1, y: 2 }) { } +d5(); // Parameter is optional as its declaration included an initializer + +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e1({x: number}) { } // x has type any NOT number +function e2({x}: { x: number }) { } // x is type number +function e3({x}: { x?: number }) { } // x is an optional with type number +function e4({x: [number,string,any] }) { } // x has type [any, any, any] +function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] + +function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; + + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts index 5a775eb69c9a0..b764e4f286c45 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts @@ -42,7 +42,7 @@ a11([1, 2, "string"]); // Error, parameter type is number[] class C { - constructor(public ...a) { } // Rest parameter can't have accessibilityModifier + constructor(public ...a) { } // Error, rest parameter can't have accessibilityModifier } // Rest parameter with generic diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts new file mode 100644 index 0000000000000..59b08013a2a94 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts @@ -0,0 +1,47 @@ +// @target: es6 + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a1(...x: (number|string)[]) { } +function a2(...a) { } +function a3(...a: Array) { } +function a4(...a: arrayString) { } +function a5(...a: stringOrNumArray) { } +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]: number[]) { } + + +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] + +a11([1, 2]); // Parameter type is number[] + +// Rest parameter with generic +function foo(...a: T[]) { } +foo("hello", 1, 2); +foo("hello", "world"); + +enum E { a, b } +const enum E1 { a, b } +function foo1(...a: T[]) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, E1.a, E.b); + + From 55f9aba85581e96d12e1c6d32d46ce148f4a1d34 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 16 Apr 2015 14:32:23 -0700 Subject: [PATCH 5/6] Move tests with errors to separate file --- ... destructuringParameterDeclaration1ES5.ts} | 25 ---- .../destructuringParameterDeclaration2.ts | 120 +++++++++--------- ... destructuringParameterDeclaration3ES5.ts} | 3 +- .../destructuringParameterDeclaration3ES6.ts | 46 +++++++ .../destructuringParameterDeclaration4.ts | 67 ++++------ .../destructuringParameterDeclaration5.ts | 50 ++++++++ ... => destructuringParameterDeclaration6.ts} | 0 7 files changed, 187 insertions(+), 124 deletions(-) rename tests/cases/conformance/es6/destructuring/{destructuringParameterDeclaration1.ts => destructuringParameterDeclaration1ES5.ts} (73%) rename tests/cases/conformance/es6/destructuring/{destructuringParameterDeclaration2ES6.ts => destructuringParameterDeclaration3ES5.ts} (91%) create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts create mode 100644 tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts rename tests/cases/conformance/es6/destructuring/{destructuringParameterDeclaration3.ts => destructuringParameterDeclaration6.ts} (100%) diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts similarity index 73% rename from tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts rename to tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts index a36e679751c4b..01f3cda52bf7d 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts @@ -10,9 +10,6 @@ function a4({x, a}: { x: number, a: number }) { } a1([1, 2, [["world"]]]); a1([1, 2, [["world"]], 3]); -a1([1, "string", [["world"]]); // Error -a1([1, 2, [["world"]], "string"]); // Error - // If the declaration includes an initializer expression (which is permitted only // when the parameter list occurs in conjunction with a function body), @@ -23,7 +20,6 @@ function b2(z = null, o = { x: 0, y: undefined }) { } function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } interface F1 { - b4(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body b5(z, y, [, a, b], {p, m: { q, r}}); } @@ -33,7 +29,6 @@ function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined b1([1, 2, 3]); // z is widen to the type any[] b2("string", { x: 200, y: "string" }); b2("string", { x: 200, y: true }); -b2("string", { x: "string", y: true }); // Error b6(["string", 1, 2]); // Shouldn't be an error b7([["string"], 1, [[true, false]]]); // Shouldn't be an error @@ -44,39 +39,28 @@ function c0({z: {x, y: {j}}}) { } function c1({z} = { z: 10 }) { } function c2({z = 10}) { } function c3({b}: { b: number|string} = { b: "hello" }) { } -function c4([z], z: number) { } // Duplicate identifier function c5([a, b, [[c]]]) { } function c6([a, b, [[c=1]]]) { } c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } -c0({z : 1}); // Error, implied type is { z: {x: any, y: {j: any}} } -c1({}); // Error, implied type is {z:number}? -c1({ z: true }); // Error, implied type is {z:number}? c1(); // Implied type is {z:number}? c1({ z: 1 }) // Implied type is {z:number}? c2({}); // Implied type is {z?: number} c2({z:1}); // Implied type is {z?: number} -c2({z:false}); // Error, implied type is {z?: number} c3({ b: 1 }); // Implied type is { b: number|string }. -c3({ b: true }); // Error, implied type is { b: number|string }. c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] -c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] - -c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer // A parameter can be marked optional by following its name or binding pattern with a question mark (?) // or by including an initializer. function d0(x?) { } function d0(x = 10) { } -function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature -function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature interface F2 { d3([a, b, c]?); @@ -97,11 +81,6 @@ class C3 implements F2 { e0([a, b, c]) { } } -class C4 implements F2 { - d3([a, b, c]?) { } - d4({x, y, c}) { } - e0([a, b, q]) { } -} function d5({x, y} = { x: 1, y: 2 }) { } d5(); // Parameter is optional as its declaration included an initializer @@ -115,7 +94,3 @@ function e2({x}: { x: number }) { } // x is type number function e3({x}: { x?: number }) { } // x is an optional with type number function e4({x: [number,string,any] }) { } // x has type [any, any, any] function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] - -function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; - - diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts index b764e4f286c45..19f508c06255e 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts @@ -1,61 +1,67 @@ -// If the parameter is a rest parameter, the parameter type is any[] -// A type annotation for a rest parameter must denote an array type. - -// RestParameter: -// ... Identifier TypeAnnotation(opt) - -type arrayString = Array -type someArray = Array | number[]; -type stringOrNumArray = Array; - -function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type -function a1(...x: (number|string)[]) { } -function a2(...a) { } -function a3(...a: Array) { } -function a4(...a: arrayString) { } -function a5(...a: stringOrNumArray) { } -function a6(...a: someArray) { } // Error, rest parameter must be array type -function a7(...b?) { } // Error, can't be optional -function a8(...b = [1,2,3]) { } // Error, can't have initializer -function a9([a, b, [[c]]]) { } -function a10([a, b, [[c]], ...x]) { } -function a11([a, b, c, ...x]: number[]) { } - - -a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a2([...array]); -a1(...array); -a1(...array2); // Error parameter type is (number|string)[] - -a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] -a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] -a9([1, 2]); // Error, parameter type is [any, any, [[any]]] - -a10([1, 2, [["string"]], false, true]); // Parameter type is any[] -a10([1, 2, 3, false, true]); // Parameter type is any[] -a10([1, 2]); // Parameter type is any[] - -a11([1, 2]); // Parameter type is number[] -a11([1, 2, "string"]); // Error, parameter type is number[] - - -class C { - constructor(public ...a) { } // Error, rest parameter can't have accessibilityModifier +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a0([a, b, [[c]]]: [number, number, string[][]]) { } +a0([1, "string", [["world"]]); // Error +a0([1, 2, [["world"]], "string"]); // Error + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +interface F1 { + b0(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body } -// Rest parameter with generic -function foo(...a: T[]) { } -foo("hello", 1, 2); // Error -foo("hello", 1, 2); -foo("hello", "world"); - -enum E { a, b } -const enum E1 { a, b } -function foo1(...a: T[]) { } -foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); -foo1(1, 2, "string", E1.a, E.b); // Error +function b1(z = null, o = { x: 0, y: undefined }) { } +function b2([a, z, y] = [undefined, null, undefined]) { } +function b3([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + +b1("string", { x: "string", y: true }); // Error + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +function c0({z: {x, y: {j}}}) { } +function c1({z} = { z: 10 }) { } +function c2({z = 10}) { } +function c3({b}: { b: number|string } = { b: "hello" }) { } +function c4([z], z: number) { } // Error Duplicate identifier +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c = 1]]]) { } + +c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} } +c1({}); // Error, implied type is {z:number}? +c1({ z: true }); // Error, implied type is {z:number}? +c2({ z: false }); // Error, implied type is {z?: number} +c3({ b: true }); // Error, implied type is { b: number|string }. +c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] +c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. Initializers (including binding property or element initializers) are +// permitted only when the parameter list occurs in conjunction with a function body + +function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature +function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature + +interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); +} + +class C4 implements F2 { + d3([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature + d4({x, y, c}) { } + e0([a, b, q]) { } +} + +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e0({x: [number, number, number]}) { } // should be an error, duplicate identifier; diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts similarity index 91% rename from tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts rename to tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts index 59b08013a2a94..6eab2225e03cf 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts @@ -30,8 +30,7 @@ a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]] a10([1, 2, [["string"]], false, true]); // Parameter type is any[] a10([1, 2, 3, false, true]); // Parameter type is any[] a10([1, 2]); // Parameter type is any[] - -a11([1, 2]); // Parameter type is number[] +a11([1, 2]); // Parameter type is number[] // Rest parameter with generic function foo(...a: T[]) { } diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts new file mode 100644 index 0000000000000..6eab2225e03cf --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts @@ -0,0 +1,46 @@ +// @target: es6 + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a1(...x: (number|string)[]) { } +function a2(...a) { } +function a3(...a: Array) { } +function a4(...a: arrayString) { } +function a5(...a: stringOrNumArray) { } +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]: number[]) { } + + +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] +a11([1, 2]); // Parameter type is number[] + +// Rest parameter with generic +function foo(...a: T[]) { } +foo("hello", 1, 2); +foo("hello", "world"); + +enum E { a, b } +const enum E1 { a, b } +function foo1(...a: T[]) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, E1.a, E.b); + + diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts index 4f0f52d30656a..02fc84b380ad6 100644 --- a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts @@ -1,49 +1,36 @@ -// Parameter with generic -interface F { } -class Class implements F { - constructor() { } -} +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. -class SubClass extends Class { - foo: boolean; - constructor() { super(); } -} +// RestParameter: +// ... Identifier TypeAnnotation(opt) -class D implements F { - foo: boolean - constructor() { } -} +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type +function a1(...x: (number|string)[]) { } +function a2(...a: someArray) { } // Error, rest parameter must be array type +function a3(...b?) { } // Error, can't be optional +function a4(...b = [1,2,3]) { } // Error, can't have initializer +function a5([a, b, [[c]]]) { } +function a6([a, b, c, ...x]: number[]) { } -class SubD extends D { - bar: number - constructor() { - super(); - } -} +a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] +a1(...array2); // Error parameter type is (number|string)[] +a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] +a5([1, 2]); // Error, parameter type is [any, any, [[any]]] +a6([1, 2, "string"]); // Error, parameter type is number[] -function d0({x} = { x: new Class() }) { } -function d1({x}: { x: F }) { } -function d2({x}: { x: Class }) { } -function d3({y}: { y: D }) { } -function d4({y} = { y: new D() }) { } -var obj = new Class(); -d0({ x: 1 }); -d0({ x: {} }); -d0({ x: "string" }); +var temp = [1, 2, 3]; +class C { + constructor(public ...temp) { } // Error, rest parameter can't have accessibilityModifier +} -d1({ x: new Class() }); -d1({ x: {} }); -d1({ x: "string" }); +// Rest parameter with generic +function foo1(...a: T[]) { } +foo1(1, 2, "string", E1.a, E.b); // Error -d2({ x: new SubClass() }); -d2({ x: {} }); -d3({ y: new SubD() }); -d3({ y: new SubClass() }); -// Error -d3({ y: new Class() }); -d3({}); -d3({ y: 1 }); -d3({ y: "world" }); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts new file mode 100644 index 0000000000000..c4fcffed49144 --- /dev/null +++ b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts @@ -0,0 +1,50 @@ +// Parameter Declaration with generic + +interface F { } +class Class implements F { + constructor() { } +} + +class SubClass extends Class { + foo: boolean; + constructor() { super(); } +} + +class D implements F { + foo: boolean + constructor() { } +} + +class SubD extends D { + bar: number + constructor() { + super(); + } +} + + +function d0({x} = { x: new Class() }) { } +function d1({x}: { x: F }) { } +function d2({x}: { x: Class }) { } +function d3({y}: { y: D }) { } +function d4({y} = { y: new D() }) { } + +var obj = new Class(); +d0({ x: 1 }); +d0({ x: {} }); +d0({ x: "string" }); + +d1({ x: new Class() }); +d1({ x: {} }); +d1({ x: "string" }); + +d2({ x: new SubClass() }); +d2({ x: {} }); + +d3({ y: new SubD() }); +d3({ y: new SubClass() }); +// Error +d3({ y: new Class() }); +d3({}); +d3({ y: 1 }); +d3({ y: "world" }); \ No newline at end of file diff --git a/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts b/tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts similarity index 100% rename from tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts rename to tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts From 3a15b3f7f4701b0e095ff237af168a648cd8031b Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 16 Apr 2015 14:53:19 -0700 Subject: [PATCH 6/6] Move error test cases into separate files --- .../diagnosticInformationMap.generated.ts | 1 + ...tructuringParameterDeclaration1.errors.txt | 272 --------------- ...cturingParameterDeclaration1ES5.errors.txt | 124 +++++++ ... destructuringParameterDeclaration1ES5.js} | 65 +--- ...tructuringParameterDeclaration1ES6.symbols | 314 ++++++++++++++++++ ...estructuringParameterDeclaration1ES6.types | 66 +++- ...tructuringParameterDeclaration2.errors.txt | 283 ++++++++++------ .../destructuringParameterDeclaration2.js | 289 ++++++++-------- ... destructuringParameterDeclaration3ES5.js} | 7 +- ...tructuringParameterDeclaration3ES5.symbols | 145 ++++++++ ...structuringParameterDeclaration3ES5.types} | 40 ++- .../destructuringParameterDeclaration3ES6.js | 80 +++++ ...tructuringParameterDeclaration3ES6.symbols | 145 ++++++++ ...estructuringParameterDeclaration3ES6.types | 206 ++++++++++++ ...tructuringParameterDeclaration4.errors.txt | 140 ++++---- .../destructuringParameterDeclaration4.js | 176 +++++----- ...tructuringParameterDeclaration5.errors.txt | 79 +++++ .../destructuringParameterDeclaration5.js | 115 +++++++ ...ructuringParameterDeclaration6.errors.txt} | 22 +- ... => destructuringParameterDeclaration6.js} | 4 +- 20 files changed, 1785 insertions(+), 788 deletions(-) delete mode 100644 tests/baselines/reference/destructuringParameterDeclaration1.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES5.errors.txt rename tests/baselines/reference/{destructuringParameterDeclaration1.js => destructuringParameterDeclaration1ES5.js} (74%) create mode 100644 tests/baselines/reference/destructuringParameterDeclaration1ES6.symbols rename tests/baselines/reference/{destructuringParameterDeclaration2ES6.js => destructuringParameterDeclaration3ES5.js} (91%) create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES5.symbols rename tests/baselines/reference/{destructuringParameterDeclaration2ES6.types => destructuringParameterDeclaration3ES5.types} (82%) create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES6.js create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES6.symbols create mode 100644 tests/baselines/reference/destructuringParameterDeclaration3ES6.types create mode 100644 tests/baselines/reference/destructuringParameterDeclaration5.errors.txt create mode 100644 tests/baselines/reference/destructuringParameterDeclaration5.js rename tests/baselines/reference/{destructuringParameterDeclaration3.errors.txt => destructuringParameterDeclaration6.errors.txt} (75%) rename tests/baselines/reference/{destructuringParameterDeclaration3.js => destructuringParameterDeclaration6.js} (90%) diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 79a5a696ef43a..f3e9c325da611 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -1,5 +1,6 @@ // /// +/* @internal */ module ts { export var Diagnostics = { Unterminated_string_literal: { code: 1002, category: DiagnosticCategory.Error, key: "Unterminated string literal." }, diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt deleted file mode 100644 index 078895d07b061..0000000000000 --- a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt +++ /dev/null @@ -1,272 +0,0 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(13,4): error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'. - Types of property '1' are incompatible. - Type 'string' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(13,29): error TS1005: ',' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(14,4): error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'. - Types of property 'pop' are incompatible. - Type '() => string | number | string[][]' is not assignable to type '() => number | string[][]'. - Type 'string | number | string[][]' is not assignable to type 'number | string[][]'. - Type 'string' is not assignable to type 'number | string[][]'. - Type 'string' is not assignable to type 'string[][]'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(26,8): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(26,16): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(36,14): error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. - Types of property 'x' are incompatible. - Type 'string' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(37,4): error TS2345: Argument of type '[string, number, number]' is not assignable to parameter of type '[undefined, null, undefined]'. - Types of property '0' are incompatible. - Type 'string' is not assignable to type 'undefined'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(38,4): error TS2345: Argument of type '[[string], number, [[boolean, boolean]]]' is not assignable to parameter of type '[[undefined], undefined, [[undefined, undefined]]]'. - Types of property '0' are incompatible. - Type '[string]' is not assignable to type '[undefined]'. - Types of property '0' are incompatible. - Type 'string' is not assignable to type 'undefined'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(47,14): error TS2300: Duplicate identifier 'z'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(47,18): error TS2300: Duplicate identifier 'z'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(53,4): error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. - Types of property 'z' are incompatible. - Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(55,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. - Property 'z' is missing in type '{}'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(56,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. - Types of property 'z' are incompatible. - Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(62,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. - Types of property 'z' are incompatible. - Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(65,4): error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. - Types of property 'b' are incompatible. - Type 'boolean' is not assignable to type 'string | number'. - Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(69,4): error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. - Types of property '2' are incompatible. - Type 'boolean' is not assignable to type '[[any]]'. - Property '0' is missing in type 'Boolean'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(71,4): error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. - Types of property '2' are incompatible. - Type '[[string]]' is not assignable to type '[[number]]'. - Types of property '0' are incompatible. - Type '[string]' is not assignable to type '[number]'. - Types of property '0' are incompatible. - Type 'string' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(76,10): error TS2393: Duplicate function implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(77,10): error TS2393: Duplicate function implementation. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(78,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(79,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(100,7): error TS2420: Class 'C4' incorrectly implements interface 'F2'. - Types of property 'd4' are incompatible. - Type '({x, y, c}: { x: any; y: any; c: any; }) => void' is not assignable to type '({x, y, z}?: { x: any; y: any; z: any; }) => any'. - Types of parameters '__0' and '__0' are incompatible. - Type '{ x: any; y: any; c: any; }' is not assignable to type '{ x: any; y: any; z: any; }'. - Property 'z' is missing in type '{ x: any; y: any; c: any; }'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts(101,8): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. - - -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1.ts (23 errors) ==== - // A parameter declaration may specify either an identifier or a binding pattern. - // The identifiers specified in parameter declarations and binding patterns - // in a parameter list must be unique within that parameter list. - - // If the declaration includes a type annotation, the parameter is of that type - function a1([a, b, [[c]]]: [number, number, string[][]]) { } - function a2(o: { x: number, a: number }) { } - function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; - function a4({x, a}: { x: number, a: number }) { } - - a1([1, 2, [["world"]]]); - a1([1, 2, [["world"]], 3]); - a1([1, "string", [["world"]]); // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'. -!!! error TS2345: Types of property '1' are incompatible. -!!! error TS2345: Type 'string' is not assignable to type 'number'. - ~ -!!! error TS1005: ',' expected. - a1([1, 2, [["world"]], "string"]); // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'. -!!! error TS2345: Types of property 'pop' are incompatible. -!!! error TS2345: Type '() => string | number | string[][]' is not assignable to type '() => number | string[][]'. -!!! error TS2345: Type 'string | number | string[][]' is not assignable to type 'number | string[][]'. -!!! error TS2345: Type 'string' is not assignable to type 'number | string[][]'. -!!! error TS2345: Type 'string' is not assignable to type 'string[][]'. - - - // If the declaration includes an initializer expression (which is permitted only - // when the parameter list occurs in conjunction with a function body), - // the parameter type is the widened form (section 3.11) of the type of the initializer expression. - - function b1(z = [undefined, null]) { }; - function b2(z = null, o = { x: 0, y: undefined }) { } - function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } - - interface F1 { - b4(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body - ~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. - b5(z, y, [, a, b], {p, m: { q, r}}); - } - - function b6([a, z, y] = [undefined, null, undefined]) { } - function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } - - b1([1, 2, 3]); // z is widen to the type any[] - b2("string", { x: 200, y: "string" }); - b2("string", { x: 200, y: true }); - b2("string", { x: "string", y: true }); // Error - ~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. -!!! error TS2345: Types of property 'x' are incompatible. -!!! error TS2345: Type 'string' is not assignable to type 'number'. - b6(["string", 1, 2]); // Shouldn't be an error - ~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '[string, number, number]' is not assignable to parameter of type '[undefined, null, undefined]'. -!!! error TS2345: Types of property '0' are incompatible. -!!! error TS2345: Type 'string' is not assignable to type 'undefined'. - b7([["string"], 1, [[true, false]]]); // Shouldn't be an error - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '[[string], number, [[boolean, boolean]]]' is not assignable to parameter of type '[[undefined], undefined, [[undefined, undefined]]]'. -!!! error TS2345: Types of property '0' are incompatible. -!!! error TS2345: Type '[string]' is not assignable to type '[undefined]'. -!!! error TS2345: Types of property '0' are incompatible. -!!! error TS2345: Type 'string' is not assignable to type 'undefined'. - - - // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) - enum Foo { a } - function c0({z: {x, y: {j}}}) { } - function c1({z} = { z: 10 }) { } - function c2({z = 10}) { } - function c3({b}: { b: number|string} = { b: "hello" }) { } - function c4([z], z: number) { } // Duplicate identifier - ~ -!!! error TS2300: Duplicate identifier 'z'. - ~ -!!! error TS2300: Duplicate identifier 'z'. - function c5([a, b, [[c]]]) { } - function c6([a, b, [[c=1]]]) { } - - c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } - c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } - c0({z : 1}); // Error, implied type is { z: {x: any, y: {j: any}} } - ~~~~~~~ -!!! error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. -!!! error TS2345: Types of property 'z' are incompatible. -!!! error TS2345: Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'. - - c1({}); // Error, implied type is {z:number}? - ~~ -!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. -!!! error TS2345: Property 'z' is missing in type '{}'. - c1({ z: true }); // Error, implied type is {z:number}? - ~~~~~~~~~~~ -!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. -!!! error TS2345: Types of property 'z' are incompatible. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. - c1(); // Implied type is {z:number}? - c1({ z: 1 }) // Implied type is {z:number}? - - c2({}); // Implied type is {z?: number} - c2({z:1}); // Implied type is {z?: number} - c2({z:false}); // Error, implied type is {z?: number} - ~~~~~~~~~ -!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. -!!! error TS2345: Types of property 'z' are incompatible. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. - - c3({ b: 1 }); // Implied type is { b: number|string }. - c3({ b: true }); // Error, implied type is { b: number|string }. - ~~~~~~~~~~~ -!!! error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. -!!! error TS2345: Types of property 'b' are incompatible. -!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. - - c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] - c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] - c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] - ~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. -!!! error TS2345: Types of property '2' are incompatible. -!!! error TS2345: Type 'boolean' is not assignable to type '[[any]]'. -!!! error TS2345: Property '0' is missing in type 'Boolean'. - - c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer - ~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. -!!! error TS2345: Types of property '2' are incompatible. -!!! error TS2345: Type '[[string]]' is not assignable to type '[[number]]'. -!!! error TS2345: Types of property '0' are incompatible. -!!! error TS2345: Type '[string]' is not assignable to type '[number]'. -!!! error TS2345: Types of property '0' are incompatible. -!!! error TS2345: Type 'string' is not assignable to type 'number'. - - // A parameter can be marked optional by following its name or binding pattern with a question mark (?) - // or by including an initializer. - - function d0(x?) { } - ~~ -!!! error TS2393: Duplicate function implementation. - function d0(x = 10) { } - ~~ -!!! error TS2393: Duplicate function implementation. - function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature - ~~~~~~~~~~ -!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. - function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature - ~~~~~~~~~~ -!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. - - interface F2 { - d3([a, b, c]?); - d4({x, y, z}?); - e0([a, b, c]); - } - - class C2 implements F2 { - constructor() { } - d3() { } - d4() { } - e0([a, b, c]) { } - } - - class C3 implements F2 { - d3([a, b, c]) { } - d4({x, y, z}) { } - e0([a, b, c]) { } - } - - class C4 implements F2 { - ~~ -!!! error TS2420: Class 'C4' incorrectly implements interface 'F2'. -!!! error TS2420: Types of property 'd4' are incompatible. -!!! error TS2420: Type '({x, y, c}: { x: any; y: any; c: any; }) => void' is not assignable to type '({x, y, z}?: { x: any; y: any; z: any; }) => any'. -!!! error TS2420: Types of parameters '__0' and '__0' are incompatible. -!!! error TS2420: Type '{ x: any; y: any; c: any; }' is not assignable to type '{ x: any; y: any; z: any; }'. -!!! error TS2420: Property 'z' is missing in type '{ x: any; y: any; c: any; }'. - d3([a, b, c]?) { } - ~~~~~~~~~~ -!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. - d4({x, y, c}) { } - e0([a, b, q]) { } - } - - function d5({x, y} = { x: 1, y: 2 }) { } - d5(); // Parameter is optional as its declaration included an initializer - - // Destructuring parameter declarations do not permit type annotations on the individual binding patterns, - // as such annotations would conflict with the already established meaning of colons in object literals. - // Type annotations must instead be written on the top- level parameter declaration - - function e1({x: number}) { } // x has type any NOT number - function e2({x}: { x: number }) { } // x is type number - function e3({x}: { x?: number }) { } // x is an optional with type number - function e4({x: [number,string,any] }) { } // x has type [any, any, any] - function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] - - function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; - - - \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES5.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1ES5.errors.txt new file mode 100644 index 0000000000000..269d2b5640da5 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES5.errors.txt @@ -0,0 +1,124 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts(32,4): error TS2345: Argument of type '[string, number, number]' is not assignable to parameter of type '[undefined, null, undefined]'. + Types of property '0' are incompatible. + Type 'string' is not assignable to type 'undefined'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts(33,4): error TS2345: Argument of type '[[string], number, [[boolean, boolean]]]' is not assignable to parameter of type '[[undefined], undefined, [[undefined, undefined]]]'. + Types of property '0' are incompatible. + Type '[string]' is not assignable to type '[undefined]'. + Types of property '0' are incompatible. + Type 'string' is not assignable to type 'undefined'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts(62,10): error TS2393: Duplicate function implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts(63,10): error TS2393: Duplicate function implementation. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES5.ts (4 errors) ==== + // A parameter declaration may specify either an identifier or a binding pattern. + // The identifiers specified in parameter declarations and binding patterns + // in a parameter list must be unique within that parameter list. + + // If the declaration includes a type annotation, the parameter is of that type + function a1([a, b, [[c]]]: [number, number, string[][]]) { } + function a2(o: { x: number, a: number }) { } + function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; + function a4({x, a}: { x: number, a: number }) { } + + a1([1, 2, [["world"]]]); + a1([1, 2, [["world"]], 3]); + + // If the declaration includes an initializer expression (which is permitted only + // when the parameter list occurs in conjunction with a function body), + // the parameter type is the widened form (section 3.11) of the type of the initializer expression. + + function b1(z = [undefined, null]) { }; + function b2(z = null, o = { x: 0, y: undefined }) { } + function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } + + interface F1 { + b5(z, y, [, a, b], {p, m: { q, r}}); + } + + function b6([a, z, y] = [undefined, null, undefined]) { } + function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + + b1([1, 2, 3]); // z is widen to the type any[] + b2("string", { x: 200, y: "string" }); + b2("string", { x: 200, y: true }); + b6(["string", 1, 2]); // Shouldn't be an error + ~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[string, number, number]' is not assignable to parameter of type '[undefined, null, undefined]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'undefined'. + b7([["string"], 1, [[true, false]]]); // Shouldn't be an error + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[[string], number, [[boolean, boolean]]]' is not assignable to parameter of type '[[undefined], undefined, [[undefined, undefined]]]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type '[string]' is not assignable to type '[undefined]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'undefined'. + + + // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) + enum Foo { a } + function c0({z: {x, y: {j}}}) { } + function c1({z} = { z: 10 }) { } + function c2({z = 10}) { } + function c3({b}: { b: number|string} = { b: "hello" }) { } + function c5([a, b, [[c]]]) { } + function c6([a, b, [[c=1]]]) { } + + c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } + c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } + + c1(); // Implied type is {z:number}? + c1({ z: 1 }) // Implied type is {z:number}? + + c2({}); // Implied type is {z?: number} + c2({z:1}); // Implied type is {z?: number} + + c3({ b: 1 }); // Implied type is { b: number|string }. + + c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] + c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] + + // A parameter can be marked optional by following its name or binding pattern with a question mark (?) + // or by including an initializer. + + function d0(x?) { } + ~~ +!!! error TS2393: Duplicate function implementation. + function d0(x = 10) { } + ~~ +!!! error TS2393: Duplicate function implementation. + + interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); + } + + class C2 implements F2 { + constructor() { } + d3() { } + d4() { } + e0([a, b, c]) { } + } + + class C3 implements F2 { + d3([a, b, c]) { } + d4({x, y, z}) { } + e0([a, b, c]) { } + } + + + function d5({x, y} = { x: 1, y: 2 }) { } + d5(); // Parameter is optional as its declaration included an initializer + + // Destructuring parameter declarations do not permit type annotations on the individual binding patterns, + // as such annotations would conflict with the already established meaning of colons in object literals. + // Type annotations must instead be written on the top- level parameter declaration + + function e1({x: number}) { } // x has type any NOT number + function e2({x}: { x: number }) { } // x is type number + function e3({x}: { x?: number }) { } // x is an optional with type number + function e4({x: [number,string,any] }) { } // x has type [any, any, any] + function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.js b/tests/baselines/reference/destructuringParameterDeclaration1ES5.js similarity index 74% rename from tests/baselines/reference/destructuringParameterDeclaration1.js rename to tests/baselines/reference/destructuringParameterDeclaration1ES5.js index 0d8590ae50a32..9a5b4eb81563f 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration1.js +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES5.js @@ -1,4 +1,4 @@ -//// [destructuringParameterDeclaration1.ts] +//// [destructuringParameterDeclaration1ES5.ts] // A parameter declaration may specify either an identifier or a binding pattern. // The identifiers specified in parameter declarations and binding patterns // in a parameter list must be unique within that parameter list. @@ -11,9 +11,6 @@ function a4({x, a}: { x: number, a: number }) { } a1([1, 2, [["world"]]]); a1([1, 2, [["world"]], 3]); -a1([1, "string", [["world"]]); // Error -a1([1, 2, [["world"]], "string"]); // Error - // If the declaration includes an initializer expression (which is permitted only // when the parameter list occurs in conjunction with a function body), @@ -24,7 +21,6 @@ function b2(z = null, o = { x: 0, y: undefined }) { } function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } interface F1 { - b4(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body b5(z, y, [, a, b], {p, m: { q, r}}); } @@ -34,7 +30,6 @@ function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined b1([1, 2, 3]); // z is widen to the type any[] b2("string", { x: 200, y: "string" }); b2("string", { x: 200, y: true }); -b2("string", { x: "string", y: true }); // Error b6(["string", 1, 2]); // Shouldn't be an error b7([["string"], 1, [[true, false]]]); // Shouldn't be an error @@ -45,39 +40,28 @@ function c0({z: {x, y: {j}}}) { } function c1({z} = { z: 10 }) { } function c2({z = 10}) { } function c3({b}: { b: number|string} = { b: "hello" }) { } -function c4([z], z: number) { } // Duplicate identifier function c5([a, b, [[c]]]) { } function c6([a, b, [[c=1]]]) { } c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } -c0({z : 1}); // Error, implied type is { z: {x: any, y: {j: any}} } -c1({}); // Error, implied type is {z:number}? -c1({ z: true }); // Error, implied type is {z:number}? c1(); // Implied type is {z:number}? c1({ z: 1 }) // Implied type is {z:number}? c2({}); // Implied type is {z?: number} c2({z:1}); // Implied type is {z?: number} -c2({z:false}); // Error, implied type is {z?: number} c3({ b: 1 }); // Implied type is { b: number|string }. -c3({ b: true }); // Error, implied type is { b: number|string }. c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] -c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] - -c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer // A parameter can be marked optional by following its name or binding pattern with a question mark (?) // or by including an initializer. function d0(x?) { } function d0(x = 10) { } -function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature -function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature interface F2 { d3([a, b, c]?); @@ -98,11 +82,6 @@ class C3 implements F2 { e0([a, b, c]) { } } -class C4 implements F2 { - d3([a, b, c]?) { } - d4({x, y, c}) { } - e0([a, b, q]) { } -} function d5({x, y} = { x: 1, y: 2 }) { } d5(); // Parameter is optional as its declaration included an initializer @@ -116,13 +95,9 @@ function e2({x}: { x: number }) { } // x is type number function e3({x}: { x?: number }) { } // x is an optional with type number function e4({x: [number,string,any] }) { } // x has type [any, any, any] function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] - -function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; - - -//// [destructuringParameterDeclaration1.js] +//// [destructuringParameterDeclaration1ES5.js] // A parameter declaration may specify either an identifier or a binding pattern. // The identifiers specified in parameter declarations and binding patterns // in a parameter list must be unique within that parameter list. @@ -140,8 +115,6 @@ function a4(_a) { } a1([1, 2, [["world"]]]); a1([1, 2, [["world"]], 3]); -a1([1, "string", [["world"]]]); // Error -a1([1, 2, [["world"]], "string"]); // Error // If the declaration includes an initializer expression (which is permitted only // when the parameter list occurs in conjunction with a function body), // the parameter type is the widened form (section 3.11) of the type of the initializer expression. @@ -165,7 +138,6 @@ function b7(_a) { b1([1, 2, 3]); // z is widen to the type any[] b2("string", { x: 200, y: "string" }); b2("string", { x: 200, y: true }); -b2("string", { x: "string", y: true }); // Error b6(["string", 1, 2]); // Shouldn't be an error b7([["string"], 1, [[true, false]]]); // Shouldn't be an error // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) @@ -185,9 +157,6 @@ function c2(_a) { function c3(_a) { var b = (_a === void 0 ? { b: "hello" } : _a).b; } -function c4(_a, z) { - var z = _a[0]; -} // Duplicate identifier function c5(_a) { var a = _a[0], b = _a[1], c = _a[2][0][0]; } @@ -196,32 +165,19 @@ function c6(_a) { } c0({ z: { x: 1, y: { j: "world" } } }); // Implied type is { z: {x: any, y: {j: any}} } c0({ z: { x: "string", y: { j: true } } }); // Implied type is { z: {x: any, y: {j: any}} } -c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} } -c1({}); // Error, implied type is {z:number}? -c1({ z: true }); // Error, implied type is {z:number}? c1(); // Implied type is {z:number}? c1({ z: 1 }); // Implied type is {z:number}? c2({}); // Implied type is {z?: number} c2({ z: 1 }); // Implied type is {z?: number} -c2({ z: false }); // Error, implied type is {z?: number} c3({ b: 1 }); // Implied type is { b: number|string }. -c3({ b: true }); // Error, implied type is { b: number|string }. c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] -c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] -c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer // A parameter can be marked optional by following its name or binding pattern with a question mark (?) // or by including an initializer. function d0(x) { } function d0(x) { if (x === void 0) { x = 10; } } -function d1(_a) { - var a = _a[0], b = _a[1], c = _a[2]; -} // Error, binding pattern can't be optional in implementation signature -function d2(_a) { - var x = _a.x, y = _a.y, z = _a.z; -} // Error, binding pattern can't be optional in implementation signature var C2 = (function () { function C2() { } @@ -246,20 +202,6 @@ var C3 = (function () { }; return C3; })(); -var C4 = (function () { - function C4() { - } - C4.prototype.d3 = function (_a) { - var a = _a[0], b = _a[1], c = _a[2]; - }; - C4.prototype.d4 = function (_a) { - var x = _a.x, y = _a.y, c = _a.c; - }; - C4.prototype.e0 = function (_a) { - var a = _a[0], b = _a[1], q = _a[2]; - }; - return C4; -})(); function d5(_a) { var _b = _a === void 0 ? { x: 1, y: 2 } : _a, x = _b.x, y = _b.y; } @@ -282,6 +224,3 @@ function e4(_a) { function e5(_a) { var _b = _a.x, a = _b[0], b = _b[1], c = _b[2]; } // x has type [any, any, any] -function e6(_a) { - var _b = _a.x, number = _b[0], number = _b[1], number = _b[2]; -} // should be an error, duplicate identifier; diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.symbols b/tests/baselines/reference/destructuringParameterDeclaration1ES6.symbols new file mode 100644 index 0000000000000..3267d4470dbf3 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES6.symbols @@ -0,0 +1,314 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts === +// Conformance for emitting ES6 + +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a1([a, b, [[c]]]: [number, number, string[][]]) { } +>a1 : Symbol(a1, Decl(destructuringParameterDeclaration1ES6.ts, 0, 0)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 7, 13)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 7, 15)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 7, 21)) + +function a2(o: { x: number, a: number }) { } +>a2 : Symbol(a2, Decl(destructuringParameterDeclaration1ES6.ts, 7, 60)) +>o : Symbol(o, Decl(destructuringParameterDeclaration1ES6.ts, 8, 12)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 8, 16)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 8, 27)) + +function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; +>a3 : Symbol(a3, Decl(destructuringParameterDeclaration1ES6.ts, 8, 44)) +>j : Symbol(j, Decl(destructuringParameterDeclaration1ES6.ts, 9, 13)) +>k : Symbol(k, Decl(destructuringParameterDeclaration1ES6.ts, 9, 15)) +>m : Symbol(m, Decl(destructuringParameterDeclaration1ES6.ts, 9, 23)) +>n : Symbol(n, Decl(destructuringParameterDeclaration1ES6.ts, 9, 25)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 9, 34)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 9, 36)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 9, 39)) +>j : Symbol(j, Decl(destructuringParameterDeclaration1ES6.ts, 9, 46)) +>k : Symbol(k, Decl(destructuringParameterDeclaration1ES6.ts, 9, 57)) +>l : Symbol(l, Decl(destructuringParameterDeclaration1ES6.ts, 9, 68)) +>m : Symbol(m, Decl(destructuringParameterDeclaration1ES6.ts, 9, 73)) +>n : Symbol(n, Decl(destructuringParameterDeclaration1ES6.ts, 9, 85)) +>q : Symbol(q, Decl(destructuringParameterDeclaration1ES6.ts, 9, 98)) + +function a4({x, a}: { x: number, a: number }) { } +>a4 : Symbol(a4, Decl(destructuringParameterDeclaration1ES6.ts, 9, 127)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 10, 13)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 10, 15)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 10, 21)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 10, 32)) + +a1([1, 2, [["world"]]]); +>a1 : Symbol(a1, Decl(destructuringParameterDeclaration1ES6.ts, 0, 0)) + +a1([1, 2, [["world"]], 3]); +>a1 : Symbol(a1, Decl(destructuringParameterDeclaration1ES6.ts, 0, 0)) + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +function b1(z = [undefined, null]) { }; +>b1 : Symbol(b1, Decl(destructuringParameterDeclaration1ES6.ts, 13, 27)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 20, 12)) +>undefined : Symbol(undefined) + +function b2(z = null, o = { x: 0, y: undefined }) { } +>b2 : Symbol(b2, Decl(destructuringParameterDeclaration1ES6.ts, 20, 39)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 21, 12)) +>o : Symbol(o, Decl(destructuringParameterDeclaration1ES6.ts, 21, 21)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 21, 27)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 21, 33)) +>undefined : Symbol(undefined) + +function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } +>b3 : Symbol(b3, Decl(destructuringParameterDeclaration1ES6.ts, 21, 53)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 22, 17)) +>j : Symbol(j, Decl(destructuringParameterDeclaration1ES6.ts, 22, 24)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 22, 32)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 22, 37)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 22, 46)) +>j : Symbol(j, Decl(destructuringParameterDeclaration1ES6.ts, 22, 51)) + +interface F1 { +>F1 : Symbol(F1, Decl(destructuringParameterDeclaration1ES6.ts, 22, 67)) + + b5(z, y, [, a, b], {p, m: { q, r}}); +>b5 : Symbol(b5, Decl(destructuringParameterDeclaration1ES6.ts, 24, 14)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 25, 7)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 25, 9)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 25, 15)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 25, 18)) +>p : Symbol(p, Decl(destructuringParameterDeclaration1ES6.ts, 25, 24)) +>q : Symbol(q, Decl(destructuringParameterDeclaration1ES6.ts, 25, 31)) +>r : Symbol(r, Decl(destructuringParameterDeclaration1ES6.ts, 25, 34)) +} + +function b6([a, z, y] = [undefined, null, undefined]) { } +>b6 : Symbol(b6, Decl(destructuringParameterDeclaration1ES6.ts, 26, 1)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 28, 13)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 28, 15)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 28, 18)) +>undefined : Symbol(undefined) +>undefined : Symbol(undefined) + +function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } +>b7 : Symbol(b7, Decl(destructuringParameterDeclaration1ES6.ts, 28, 57)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 29, 14)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 29, 17)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 29, 23)) +>d : Symbol(d, Decl(destructuringParameterDeclaration1ES6.ts, 29, 25)) +>undefined : Symbol(undefined) +>undefined : Symbol(undefined) +>undefined : Symbol(undefined) +>undefined : Symbol(undefined) + +b1([1, 2, 3]); // z is widen to the type any[] +>b1 : Symbol(b1, Decl(destructuringParameterDeclaration1ES6.ts, 13, 27)) + +b2("string", { x: 200, y: "string" }); +>b2 : Symbol(b2, Decl(destructuringParameterDeclaration1ES6.ts, 20, 39)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 32, 14)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 32, 22)) + +b2("string", { x: 200, y: true }); +>b2 : Symbol(b2, Decl(destructuringParameterDeclaration1ES6.ts, 20, 39)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 33, 14)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 33, 22)) + + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +enum Foo { a } +>Foo : Symbol(Foo, Decl(destructuringParameterDeclaration1ES6.ts, 33, 34)) +>a : Symbol(Foo.a, Decl(destructuringParameterDeclaration1ES6.ts, 37, 10)) + +function c0({z: {x, y: {j}}}) { } +>c0 : Symbol(c0, Decl(destructuringParameterDeclaration1ES6.ts, 37, 14)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 38, 17)) +>j : Symbol(j, Decl(destructuringParameterDeclaration1ES6.ts, 38, 24)) + +function c1({z} = { z: 10 }) { } +>c1 : Symbol(c1, Decl(destructuringParameterDeclaration1ES6.ts, 38, 33)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 39, 13)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 39, 19)) + +function c2({z = 10}) { } +>c2 : Symbol(c2, Decl(destructuringParameterDeclaration1ES6.ts, 39, 32)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 40, 13)) + +function c3({b}: { b: number|string} = { b: "hello" }) { } +>c3 : Symbol(c3, Decl(destructuringParameterDeclaration1ES6.ts, 40, 25)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 41, 13)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 41, 18)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 41, 40)) + +function c5([a, b, [[c]]]) { } +>c5 : Symbol(c5, Decl(destructuringParameterDeclaration1ES6.ts, 41, 58)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 42, 13)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 42, 15)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 42, 21)) + +function c6([a, b, [[c=1]]]) { } +>c6 : Symbol(c6, Decl(destructuringParameterDeclaration1ES6.ts, 42, 30)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 43, 13)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 43, 15)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 43, 21)) + +c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } +>c0 : Symbol(c0, Decl(destructuringParameterDeclaration1ES6.ts, 37, 14)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 45, 4)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 45, 9)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 45, 15)) +>j : Symbol(j, Decl(destructuringParameterDeclaration1ES6.ts, 45, 20)) + +c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } +>c0 : Symbol(c0, Decl(destructuringParameterDeclaration1ES6.ts, 37, 14)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 46, 4)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 46, 9)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 46, 22)) +>j : Symbol(j, Decl(destructuringParameterDeclaration1ES6.ts, 46, 27)) + +c1(); // Implied type is {z:number}? +>c1 : Symbol(c1, Decl(destructuringParameterDeclaration1ES6.ts, 38, 33)) + +c1({ z: 1 }) // Implied type is {z:number}? +>c1 : Symbol(c1, Decl(destructuringParameterDeclaration1ES6.ts, 38, 33)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 49, 4)) + +c2({}); // Implied type is {z?: number} +>c2 : Symbol(c2, Decl(destructuringParameterDeclaration1ES6.ts, 39, 32)) + +c2({z:1}); // Implied type is {z?: number} +>c2 : Symbol(c2, Decl(destructuringParameterDeclaration1ES6.ts, 39, 32)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 52, 4)) + +c3({ b: 1 }); // Implied type is { b: number|string }. +>c3 : Symbol(c3, Decl(destructuringParameterDeclaration1ES6.ts, 40, 25)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 54, 4)) + +c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] +>c5 : Symbol(c5, Decl(destructuringParameterDeclaration1ES6.ts, 41, 58)) + +c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] +>c5 : Symbol(c5, Decl(destructuringParameterDeclaration1ES6.ts, 41, 58)) + + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. + +interface F2 { +>F2 : Symbol(F2, Decl(destructuringParameterDeclaration1ES6.ts, 57, 38)) + + d3([a, b, c]?); +>d3 : Symbol(d3, Decl(destructuringParameterDeclaration1ES6.ts, 63, 14)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 64, 8)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 64, 10)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 64, 13)) + + d4({x, y, z}?); +>d4 : Symbol(d4, Decl(destructuringParameterDeclaration1ES6.ts, 64, 19)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 65, 8)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 65, 10)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 65, 13)) + + e0([a, b, c]); +>e0 : Symbol(e0, Decl(destructuringParameterDeclaration1ES6.ts, 65, 19)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 66, 8)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 66, 10)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 66, 13)) +} + +class C2 implements F2 { +>C2 : Symbol(C2, Decl(destructuringParameterDeclaration1ES6.ts, 67, 1)) +>F2 : Symbol(F2, Decl(destructuringParameterDeclaration1ES6.ts, 57, 38)) + + constructor() { } + d3() { } +>d3 : Symbol(d3, Decl(destructuringParameterDeclaration1ES6.ts, 70, 21)) + + d4() { } +>d4 : Symbol(d4, Decl(destructuringParameterDeclaration1ES6.ts, 71, 12)) + + e0([a, b, c]) { } +>e0 : Symbol(e0, Decl(destructuringParameterDeclaration1ES6.ts, 72, 12)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 73, 8)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 73, 10)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 73, 13)) +} + +class C3 implements F2 { +>C3 : Symbol(C3, Decl(destructuringParameterDeclaration1ES6.ts, 74, 1)) +>F2 : Symbol(F2, Decl(destructuringParameterDeclaration1ES6.ts, 57, 38)) + + d3([a, b, c]) { } +>d3 : Symbol(d3, Decl(destructuringParameterDeclaration1ES6.ts, 76, 24)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 77, 8)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 77, 10)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 77, 13)) + + d4({x, y, z}) { } +>d4 : Symbol(d4, Decl(destructuringParameterDeclaration1ES6.ts, 77, 21)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 78, 8)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 78, 10)) +>z : Symbol(z, Decl(destructuringParameterDeclaration1ES6.ts, 78, 13)) + + e0([a, b, c]) { } +>e0 : Symbol(e0, Decl(destructuringParameterDeclaration1ES6.ts, 78, 21)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 79, 8)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 79, 10)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 79, 13)) +} + +function d5({x, y} = { x: 1, y: 2 }) { } +>d5 : Symbol(d5, Decl(destructuringParameterDeclaration1ES6.ts, 80, 1)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 82, 13)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 82, 15)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 82, 22)) +>y : Symbol(y, Decl(destructuringParameterDeclaration1ES6.ts, 82, 28)) + +d5(); // Parameter is optional as its declaration included an initializer +>d5 : Symbol(d5, Decl(destructuringParameterDeclaration1ES6.ts, 80, 1)) + +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration + +function e1({x: number}) { } // x has type any NOT number +>e1 : Symbol(e1, Decl(destructuringParameterDeclaration1ES6.ts, 83, 5)) +>number : Symbol(number, Decl(destructuringParameterDeclaration1ES6.ts, 89, 13)) + +function e2({x}: { x: number }) { } // x is type number +>e2 : Symbol(e2, Decl(destructuringParameterDeclaration1ES6.ts, 89, 28)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 90, 13)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 90, 18)) + +function e3({x}: { x?: number }) { } // x is an optional with type number +>e3 : Symbol(e3, Decl(destructuringParameterDeclaration1ES6.ts, 90, 35)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 91, 13)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 91, 18)) + +function e4({x: [number,string,any] }) { } // x has type [any, any, any] +>e4 : Symbol(e4, Decl(destructuringParameterDeclaration1ES6.ts, 91, 36)) +>number : Symbol(number, Decl(destructuringParameterDeclaration1ES6.ts, 92, 17)) +>string : Symbol(string, Decl(destructuringParameterDeclaration1ES6.ts, 92, 24)) +>any : Symbol(any, Decl(destructuringParameterDeclaration1ES6.ts, 92, 31)) + +function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] +>e5 : Symbol(e5, Decl(destructuringParameterDeclaration1ES6.ts, 92, 42)) +>a : Symbol(a, Decl(destructuringParameterDeclaration1ES6.ts, 93, 17)) +>b : Symbol(b, Decl(destructuringParameterDeclaration1ES6.ts, 93, 19)) +>c : Symbol(c, Decl(destructuringParameterDeclaration1ES6.ts, 93, 22)) +>x : Symbol(x, Decl(destructuringParameterDeclaration1ES6.ts, 93, 29)) + +function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; +>e6 : Symbol(e6, Decl(destructuringParameterDeclaration1ES6.ts, 93, 64)) +>number : Symbol(number, Decl(destructuringParameterDeclaration1ES6.ts, 95, 17), Decl(destructuringParameterDeclaration1ES6.ts, 95, 24), Decl(destructuringParameterDeclaration1ES6.ts, 95, 32)) +>number : Symbol(number, Decl(destructuringParameterDeclaration1ES6.ts, 95, 17), Decl(destructuringParameterDeclaration1ES6.ts, 95, 24), Decl(destructuringParameterDeclaration1ES6.ts, 95, 32)) +>number : Symbol(number, Decl(destructuringParameterDeclaration1ES6.ts, 95, 17), Decl(destructuringParameterDeclaration1ES6.ts, 95, 24), Decl(destructuringParameterDeclaration1ES6.ts, 95, 32)) + + + diff --git a/tests/baselines/reference/destructuringParameterDeclaration1ES6.types b/tests/baselines/reference/destructuringParameterDeclaration1ES6.types index ed77f2dc92b32..9395fa49b5b63 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration1ES6.types +++ b/tests/baselines/reference/destructuringParameterDeclaration1ES6.types @@ -22,10 +22,10 @@ function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boo >a3 : ({j, k, l: {m, n}, q: [a, b, c]}: { j: number; k: string; l: { m: boolean; n: number; }; q: (string | number)[]; }) => void >j : number >k : string ->l : unknown +>l : any >m : boolean >n : number ->q : unknown +>q : any >a : string | number >b : string | number >c : string | number @@ -47,15 +47,22 @@ a1([1, 2, [["world"]]]); >a1([1, 2, [["world"]]]) : void >a1 : ([a, b, [[c]]]: [number, number, string[][]]) => void >[1, 2, [["world"]]] : [number, number, string[][]] +>1 : number +>2 : number >[["world"]] : string[][] >["world"] : string[] +>"world" : string a1([1, 2, [["world"]], 3]); >a1([1, 2, [["world"]], 3]) : void >a1 : ([a, b, [[c]]]: [number, number, string[][]]) => void >[1, 2, [["world"]], 3] : [number, number, string[][], number] +>1 : number +>2 : number >[["world"]] : string[][] >["world"] : string[] +>"world" : string +>3 : number // If the declaration includes an initializer expression (which is permitted only @@ -67,29 +74,34 @@ function b1(z = [undefined, null]) { }; >z : any[] >[undefined, null] : null[] >undefined : undefined +>null : null function b2(z = null, o = { x: 0, y: undefined }) { } >b2 : (z?: any, o?: { x: number; y: any; }) => void >z : any +>null : null >o : { x: number; y: any; } >{ x: 0, y: undefined } : { x: number; y: undefined; } >x : number +>0 : number >y : undefined >undefined : undefined function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } >b3 : ({z: {x, y: {j}}}?: { z: { x: string; y: { j: number; }; }; }) => void ->z : unknown +>z : any >x : string ->y : unknown +>y : any >j : number >{ z: { x: "hi", y: { j: 1 } } } : { z: { x: string; y: { j: number; }; }; } >z : { x: string; y: { j: number; }; } >{ x: "hi", y: { j: 1 } } : { x: string; y: { j: number; }; } >x : string +>"hi" : string >y : { j: number; } >{ j: 1 } : { j: number; } >j : number +>1 : number interface F1 { >F1 : F1 @@ -98,10 +110,11 @@ interface F1 { >b5 : (z: any, y: any, [, a, b]: [any, any, any], {p, m: { q, r}}: { p: any; m: { q: any; r: any; }; }) => any >z : any >y : any +> : undefined >a : any >b : any >p : any ->m : unknown +>m : any >q : any >r : any } @@ -113,6 +126,7 @@ function b6([a, z, y] = [undefined, null, undefined]) { } >y : any >[undefined, null, undefined] : [undefined, null, undefined] >undefined : undefined +>null : null >undefined : undefined function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } @@ -134,20 +148,29 @@ b1([1, 2, 3]); // z is widen to the type any[] >b1([1, 2, 3]) : void >b1 : (z?: any[]) => void >[1, 2, 3] : number[] +>1 : number +>2 : number +>3 : number b2("string", { x: 200, y: "string" }); >b2("string", { x: 200, y: "string" }) : void >b2 : (z?: any, o?: { x: number; y: any; }) => void +>"string" : string >{ x: 200, y: "string" } : { x: number; y: string; } >x : number +>200 : number >y : string +>"string" : string b2("string", { x: 200, y: true }); >b2("string", { x: 200, y: true }) : void >b2 : (z?: any, o?: { x: number; y: any; }) => void +>"string" : string >{ x: 200, y: true } : { x: number; y: boolean; } >x : number +>200 : number >y : boolean +>true : boolean // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) @@ -157,9 +180,9 @@ enum Foo { a } function c0({z: {x, y: {j}}}) { } >c0 : ({z: {x, y: {j}}}: { z: { x: any; y: { j: any; }; }; }) => void ->z : unknown +>z : any >x : any ->y : unknown +>y : any >j : any function c1({z} = { z: 10 }) { } @@ -167,10 +190,12 @@ function c1({z} = { z: 10 }) { } >z : number >{ z: 10 } : { z: number; } >z : number +>10 : number function c2({z = 10}) { } >c2 : ({z = 10}: { z?: number; }) => void >z : number +>10 : number function c3({b}: { b: number|string} = { b: "hello" }) { } >c3 : ({b}?: { b: string | number; }) => void @@ -178,6 +203,7 @@ function c3({b}: { b: number|string} = { b: "hello" }) { } >b : string | number >{ b: "hello" } : { b: string; } >b : string +>"hello" : string function c5([a, b, [[c]]]) { } >c5 : ([a, b, [[c]]]: [any, any, [[any]]]) => void @@ -190,6 +216,7 @@ function c6([a, b, [[c=1]]]) { } >a : any >b : any >c : number +>1 : number c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } >c0({z : { x: 1, y: { j: "world" } }}) : void @@ -198,9 +225,11 @@ c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: >z : { x: number; y: { j: string; }; } >{ x: 1, y: { j: "world" } } : { x: number; y: { j: string; }; } >x : number +>1 : number >y : { j: string; } >{ j: "world" } : { j: string; } >j : string +>"world" : string c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } >c0({z : { x: "string", y: { j: true } }}) : void @@ -209,9 +238,11 @@ c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: >z : { x: string; y: { j: boolean; }; } >{ x: "string", y: { j: true } } : { x: string; y: { j: boolean; }; } >x : string +>"string" : string >y : { j: boolean; } >{ j: true } : { j: boolean; } >j : boolean +>true : boolean c1(); // Implied type is {z:number}? >c1() : void @@ -222,6 +253,7 @@ c1({ z: 1 }) // Implied type is {z:number}? >c1 : ({z}?: { z: number; }) => void >{ z: 1 } : { z: number; } >z : number +>1 : number c2({}); // Implied type is {z?: number} >c2({}) : void @@ -233,26 +265,36 @@ c2({z:1}); // Implied type is {z?: number} >c2 : ({z = 10}: { z?: number; }) => void >{z:1} : { z: number; } >z : number +>1 : number c3({ b: 1 }); // Implied type is { b: number|string }. >c3({ b: 1 }) : void >c3 : ({b}?: { b: string | number; }) => void >{ b: 1 } : { b: number; } >b : number +>1 : number c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] >c5([1, 2, [["string"]]]) : void >c5 : ([a, b, [[c]]]: [any, any, [[any]]]) => void >[1, 2, [["string"]]] : [number, number, [[string]]] +>1 : number +>2 : number >[["string"]] : [[string]] >["string"] : [string] +>"string" : string c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] >c5([1, 2, [["string"]], false, true]) : void >c5 : ([a, b, [[c]]]: [any, any, [[any]]]) => void >[1, 2, [["string"]], false, true] : [number, number, [[string]], boolean, boolean] +>1 : number +>2 : number >[["string"]] : [[string]] >["string"] : [string] +>"string" : string +>false : boolean +>true : boolean // A parameter can be marked optional by following its name or binding pattern with a question mark (?) @@ -327,7 +369,9 @@ function d5({x, y} = { x: 1, y: 2 }) { } >y : number >{ x: 1, y: 2 } : { x: number; y: number; } >x : number +>1 : number >y : number +>2 : number d5(); // Parameter is optional as its declaration included an initializer >d5() : void @@ -339,7 +383,7 @@ d5(); // Parameter is optional as its declaration included an initializer function e1({x: number}) { } // x has type any NOT number >e1 : ({x: number}: { x: any; }) => void ->x : unknown +>x : any >number : any function e2({x}: { x: number }) { } // x is type number @@ -354,14 +398,14 @@ function e3({x}: { x?: number }) { } // x is an optional with type number function e4({x: [number,string,any] }) { } // x has type [any, any, any] >e4 : ({x: [number,string,any] }: { x: [any, any, any]; }) => void ->x : unknown +>x : any >number : any >string : any >any : any function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] >e5 : ({x: [a, b, c]}: { x: [number, number, number]; }) => void ->x : unknown +>x : any >a : number >b : number >c : number @@ -369,7 +413,7 @@ function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type function e6({x: [number, number, number]}) { } // should be an error, duplicate identifier; >e6 : ({x: [number, number, number]}: { x: [any, any, any]; }) => void ->x : unknown +>x : any >number : any >number : any >number : any diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt index 60f8560455901..11af7b0d17c24 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration2.errors.txt @@ -1,117 +1,194 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(11,13): error TS2370: A rest parameter must be of an array type. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(17,13): error TS2370: A rest parameter must be of an array type. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(25,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. - Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(30,4): error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. - Type 'boolean' is not assignable to type 'string | number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,4): error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'. + Types of property '1' are incompatible. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(7,29): error TS1005: ',' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(8,4): error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'. + Types of property 'pop' are incompatible. + Type '() => string | number | string[][]' is not assignable to type '() => number | string[][]'. + Type 'string | number | string[][]' is not assignable to type 'number | string[][]'. + Type 'string' is not assignable to type 'number | string[][]'. + Type 'string' is not assignable to type 'string[][]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(16,8): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(16,16): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(23,14): error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. + Types of property 'x' are incompatible. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(30,14): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(30,18): error TS2300: Duplicate identifier 'z'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(34,4): error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. + Types of property 'z' are incompatible. + Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(35,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. + Property 'z' is missing in type '{}'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(36,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. + Types of property 'z' are incompatible. Type 'boolean' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(33,4): error TS2345: Argument of type '[number, number, string, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(37,4): error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. + Types of property 'z' are incompatible. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(38,4): error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. + Types of property 'b' are incompatible. + Type 'boolean' is not assignable to type 'string | number'. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(39,4): error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. Types of property '2' are incompatible. - Type 'string' is not assignable to type '[[any]]'. - Property '0' is missing in type 'String'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(34,4): error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]]]'. - Property '2' is missing in type '[number, number]'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(41,5): error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'. - Type 'string | number' is not assignable to type 'number'. - Type 'string' is not assignable to type 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(45,24): error TS1005: ',' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(50,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. - Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(59,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. - Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'. + Type 'boolean' is not assignable to type '[[any]]'. + Property '0' is missing in type 'Boolean'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(40,4): error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. + Types of property '2' are incompatible. + Type '[[string]]' is not assignable to type '[[number]]'. + Types of property '0' are incompatible. + Type '[string]' is not assignable to type '[number]'. + Types of property '0' are incompatible. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(46,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(47,13): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(55,7): error TS2420: Class 'C4' incorrectly implements interface 'F2'. + Types of property 'd4' are incompatible. + Type '({x, y, c}: { x: any; y: any; c: any; }) => void' is not assignable to type '({x, y, z}?: { x: any; y: any; z: any; }) => any'. + Types of parameters '__0' and '__0' are incompatible. + Type '{ x: any; y: any; c: any; }' is not assignable to type '{ x: any; y: any; z: any; }'. + Property 'z' is missing in type '{ x: any; y: any; c: any; }'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts(56,8): error TS2463: A binding pattern parameter cannot be optional in an implementation signature. -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts (10 errors) ==== - // If the parameter is a rest parameter, the parameter type is any[] - // A type annotation for a rest parameter must denote an array type. - - // RestParameter: - // ... Identifier TypeAnnotation(opt) - - type arrayString = Array - type someArray = Array | number[]; - type stringOrNumArray = Array; - - function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2370: A rest parameter must be of an array type. - function a1(...x: (number|string)[]) { } - function a2(...a) { } - function a3(...a: Array) { } - function a4(...a: arrayString) { } - function a5(...a: stringOrNumArray) { } - function a6(...a: someArray) { } // Error, rest parameter must be array type - ~~~~~~~~~~~~~~~ -!!! error TS2370: A rest parameter must be of an array type. - function a7(...b?) { } // Error, can't be optional - function a8(...b = [1,2,3]) { } // Error, can't have initializer - function a9([a, b, [[c]]]) { } - function a10([a, b, [[c]], ...x]) { } - function a11([a, b, c, ...x]: number[]) { } - - - a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] - ~~~~ -!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. - var array = [1, 2, 3]; - var array2 = [true, false, "hello"]; - a2([...array]); - a1(...array); - a1(...array2); // Error parameter type is (number|string)[] - ~~~~~~~~~ -!!! error TS2345: Argument of type 'string | boolean' is not assignable to parameter of type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. -!!! error TS2345: Type 'boolean' is not assignable to type 'number'. +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2.ts (19 errors) ==== + // A parameter declaration may specify either an identifier or a binding pattern. + // The identifiers specified in parameter declarations and binding patterns + // in a parameter list must be unique within that parameter list. - a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] - a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] + // If the declaration includes a type annotation, the parameter is of that type + function a0([a, b, [[c]]]: [number, number, string[][]]) { } + a0([1, "string", [["world"]]); // Error + ~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[number, string, string[][]]' is not assignable to parameter of type '[number, number, string[][]]'. +!!! error TS2345: Types of property '1' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'number'. + ~ +!!! error TS1005: ',' expected. + a0([1, 2, [["world"]], "string"]); // Error ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '[number, number, string, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. -!!! error TS2345: Types of property '2' are incompatible. -!!! error TS2345: Type 'string' is not assignable to type '[[any]]'. -!!! error TS2345: Property '0' is missing in type 'String'. - a9([1, 2]); // Error, parameter type is [any, any, [[any]]] - ~~~~~~ -!!! error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]]]'. -!!! error TS2345: Property '2' is missing in type '[number, number]'. - - a10([1, 2, [["string"]], false, true]); // Parameter type is any[] - a10([1, 2, 3, false, true]); // Parameter type is any[] - a10([1, 2]); // Parameter type is any[] - - a11([1, 2]); // Parameter type is number[] - a11([1, 2, "string"]); // Error, parameter type is number[] - ~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'. -!!! error TS2345: Type 'string | number' is not assignable to type 'number'. +!!! error TS2345: Argument of type '[number, number, string[][], string]' is not assignable to parameter of type '[number, number, string[][]]'. +!!! error TS2345: Types of property 'pop' are incompatible. +!!! error TS2345: Type '() => string | number | string[][]' is not assignable to type '() => number | string[][]'. +!!! error TS2345: Type 'string | number | string[][]' is not assignable to type 'number | string[][]'. +!!! error TS2345: Type 'string' is not assignable to type 'number | string[][]'. +!!! error TS2345: Type 'string' is not assignable to type 'string[][]'. + + + // If the declaration includes an initializer expression (which is permitted only + // when the parameter list occurs in conjunction with a function body), + // the parameter type is the widened form (section 3.11) of the type of the initializer expression. + + interface F1 { + b0(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body + ~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + } + + function b1(z = null, o = { x: 0, y: undefined }) { } + function b2([a, z, y] = [undefined, null, undefined]) { } + function b3([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + + b1("string", { x: "string", y: true }); // Error + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ x: string; y: boolean; }' is not assignable to parameter of type '{ x: number; y: any; }'. +!!! error TS2345: Types of property 'x' are incompatible. !!! error TS2345: Type 'string' is not assignable to type 'number'. + // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) + function c0({z: {x, y: {j}}}) { } + function c1({z} = { z: 10 }) { } + function c2({z = 10}) { } + function c3({b}: { b: number|string } = { b: "hello" }) { } + function c4([z], z: number) { } // Error Duplicate identifier + ~ +!!! error TS2300: Duplicate identifier 'z'. + ~ +!!! error TS2300: Duplicate identifier 'z'. + function c5([a, b, [[c]]]) { } + function c6([a, b, [[c = 1]]]) { } - class C { - constructor(public ...a) { } // Error, rest parameter can't have accessibilityModifier - ~~~ -!!! error TS1005: ',' expected. + c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} } + ~~~~~~~~ +!!! error TS2345: Argument of type '{ z: number; }' is not assignable to parameter of type '{ z: { x: any; y: { j: any; }; }; }'. +!!! error TS2345: Types of property 'z' are incompatible. +!!! error TS2345: Type 'number' is not assignable to type '{ x: any; y: { j: any; }; }'. + c1({}); // Error, implied type is {z:number}? + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ z: number; }'. +!!! error TS2345: Property 'z' is missing in type '{}'. + c1({ z: true }); // Error, implied type is {z:number}? + ~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z: number; }'. +!!! error TS2345: Types of property 'z' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + c2({ z: false }); // Error, implied type is {z?: number} + ~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ z: boolean; }' is not assignable to parameter of type '{ z?: number; }'. +!!! error TS2345: Types of property 'z' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + c3({ b: true }); // Error, implied type is { b: number|string }. + ~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ b: boolean; }' is not assignable to parameter of type '{ b: string | number; }'. +!!! error TS2345: Types of property 'b' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] + ~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[number, number, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. +!!! error TS2345: Types of property '2' are incompatible. +!!! error TS2345: Type 'boolean' is not assignable to type '[[any]]'. +!!! error TS2345: Property '0' is missing in type 'Boolean'. + c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[number, number, [[string]]]' is not assignable to parameter of type '[any, any, [[number]]]'. +!!! error TS2345: Types of property '2' are incompatible. +!!! error TS2345: Type '[[string]]' is not assignable to type '[[number]]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type '[string]' is not assignable to type '[number]'. +!!! error TS2345: Types of property '0' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'number'. + + // A parameter can be marked optional by following its name or binding pattern with a question mark (?) + // or by including an initializer. Initializers (including binding property or element initializers) are + // permitted only when the parameter list occurs in conjunction with a function body + + function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature + ~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature + ~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + + interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); } - // Rest parameter with generic - function foo(...a: T[]) { } - foo("hello", 1, 2); // Error - ~~~ -!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. -!!! error TS2453: Type argument candidate 'string' is not a valid type argument because it is not a supertype of candidate 'number'. - foo("hello", 1, 2); - foo("hello", "world"); - - enum E { a, b } - const enum E1 { a, b } - function foo1(...a: T[]) { } - foo1(1, 2, 3, E.a); - foo1(1, 2, 3, E1.a, E.b); - foo1(1, 2, "string", E1.a, E.b); // Error - ~~~~ -!!! error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly. -!!! error TS2453: Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'. + class C4 implements F2 { + ~~ +!!! error TS2420: Class 'C4' incorrectly implements interface 'F2'. +!!! error TS2420: Types of property 'd4' are incompatible. +!!! error TS2420: Type '({x, y, c}: { x: any; y: any; c: any; }) => void' is not assignable to type '({x, y, z}?: { x: any; y: any; z: any; }) => any'. +!!! error TS2420: Types of parameters '__0' and '__0' are incompatible. +!!! error TS2420: Type '{ x: any; y: any; c: any; }' is not assignable to type '{ x: any; y: any; z: any; }'. +!!! error TS2420: Property 'z' is missing in type '{ x: any; y: any; c: any; }'. + d3([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature + ~~~~~~~~~~ +!!! error TS2463: A binding pattern parameter cannot be optional in an implementation signature. + d4({x, y, c}) { } + e0([a, b, q]) { } + } + + // Destructuring parameter declarations do not permit type annotations on the individual binding patterns, + // as such annotations would conflict with the already established meaning of colons in object literals. + // Type annotations must instead be written on the top- level parameter declaration + + function e0({x: [number, number, number]}) { } // should be an error, duplicate identifier; \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration2.js b/tests/baselines/reference/destructuringParameterDeclaration2.js index 187966c35970b..8033ff7eeae86 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2.js +++ b/tests/baselines/reference/destructuringParameterDeclaration2.js @@ -1,178 +1,149 @@ //// [destructuringParameterDeclaration2.ts] -// If the parameter is a rest parameter, the parameter type is any[] -// A type annotation for a rest parameter must denote an array type. - -// RestParameter: -// ... Identifier TypeAnnotation(opt) - -type arrayString = Array -type someArray = Array | number[]; -type stringOrNumArray = Array; - -function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type -function a1(...x: (number|string)[]) { } -function a2(...a) { } -function a3(...a: Array) { } -function a4(...a: arrayString) { } -function a5(...a: stringOrNumArray) { } -function a6(...a: someArray) { } // Error, rest parameter must be array type -function a7(...b?) { } // Error, can't be optional -function a8(...b = [1,2,3]) { } // Error, can't have initializer -function a9([a, b, [[c]]]) { } -function a10([a, b, [[c]], ...x]) { } -function a11([a, b, c, ...x]: number[]) { } - - -a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a2([...array]); -a1(...array); -a1(...array2); // Error parameter type is (number|string)[] - -a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] -a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] -a9([1, 2]); // Error, parameter type is [any, any, [[any]]] - -a10([1, 2, [["string"]], false, true]); // Parameter type is any[] -a10([1, 2, 3, false, true]); // Parameter type is any[] -a10([1, 2]); // Parameter type is any[] - -a11([1, 2]); // Parameter type is number[] -a11([1, 2, "string"]); // Error, parameter type is number[] - - -class C { - constructor(public ...a) { } // Error, rest parameter can't have accessibilityModifier +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. + +// If the declaration includes a type annotation, the parameter is of that type +function a0([a, b, [[c]]]: [number, number, string[][]]) { } +a0([1, "string", [["world"]]); // Error +a0([1, 2, [["world"]], "string"]); // Error + + +// If the declaration includes an initializer expression (which is permitted only +// when the parameter list occurs in conjunction with a function body), +// the parameter type is the widened form (section 3.11) of the type of the initializer expression. + +interface F1 { + b0(z = 10, [[a, b], d, {u}] = [[1, 2], "string", { u: false }]); // Error, no function body +} + +function b1(z = null, o = { x: 0, y: undefined }) { } +function b2([a, z, y] = [undefined, null, undefined]) { } +function b3([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } + +b1("string", { x: "string", y: true }); // Error + +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +function c0({z: {x, y: {j}}}) { } +function c1({z} = { z: 10 }) { } +function c2({z = 10}) { } +function c3({b}: { b: number|string } = { b: "hello" }) { } +function c4([z], z: number) { } // Error Duplicate identifier +function c5([a, b, [[c]]]) { } +function c6([a, b, [[c = 1]]]) { } + +c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} } +c1({}); // Error, implied type is {z:number}? +c1({ z: true }); // Error, implied type is {z:number}? +c2({ z: false }); // Error, implied type is {z?: number} +c3({ b: true }); // Error, implied type is { b: number|string }. +c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] +c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer + +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. Initializers (including binding property or element initializers) are +// permitted only when the parameter list occurs in conjunction with a function body + +function d1([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature +function d2({x, y, z}?) { } // Error, binding pattern can't be optional in implementation signature + +interface F2 { + d3([a, b, c]?); + d4({x, y, z}?); + e0([a, b, c]); } -// Rest parameter with generic -function foo(...a: T[]) { } -foo("hello", 1, 2); // Error -foo("hello", 1, 2); -foo("hello", "world"); +class C4 implements F2 { + d3([a, b, c]?) { } // Error, binding pattern can't be optional in implementation signature + d4({x, y, c}) { } + e0([a, b, q]) { } +} + +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration -enum E { a, b } -const enum E1 { a, b } -function foo1(...a: T[]) { } -foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); -foo1(1, 2, "string", E1.a, E.b); // Error +function e0({x: [number, number, number]}) { } // should be an error, duplicate identifier; //// [destructuringParameterDeclaration2.js] -// If the parameter is a rest parameter, the parameter type is any[] -// A type annotation for a rest parameter must denote an array type. -function a0() { - var x = []; - for (var _i = 0; _i < arguments.length; _i++) { - x[_i - 0] = arguments[_i]; - } -} // Error, rest parameter must be array type -function a1() { - var x = []; - for (var _i = 0; _i < arguments.length; _i++) { - x[_i - 0] = arguments[_i]; - } +// A parameter declaration may specify either an identifier or a binding pattern. +// The identifiers specified in parameter declarations and binding patterns +// in a parameter list must be unique within that parameter list. +// If the declaration includes a type annotation, the parameter is of that type +function a0(_a) { + var a = _a[0], b = _a[1], c = _a[2][0][0]; } -function a2() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +a0([1, "string", [["world"]]]); // Error +a0([1, 2, [["world"]], "string"]); // Error +function b1(z, o) { + if (z === void 0) { z = null; } + if (o === void 0) { o = { x: 0, y: undefined }; } } -function a3() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +function b2(_a) { + var _b = _a === void 0 ? [undefined, null, undefined] : _a, a = _b[0], z = _b[1], y = _b[2]; } -function a4() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +function b3(_a) { + var _b = _a === void 0 ? [[undefined], undefined, [[undefined, undefined]]] : _a, a = _b[0][0], b = _b[1], _c = _b[2][0], c = _c[0], d = _c[1]; } -function a5() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +b1("string", { x: "string", y: true }); // Error +// If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) +function c0(_a) { + var _b = _a.z, x = _b.x, j = _b.y.j; } -function a6() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } -} // Error, rest parameter must be array type -function a7() { - var b = []; - for (var _i = 0; _i < arguments.length; _i++) { - b[_i - 0] = arguments[_i]; - } -} // Error, can't be optional -function a8() { - if (b === void 0) { b = [1, 2, 3]; } - var b = []; - for (var _i = 0; _i < arguments.length; _i++) { - b[_i - 0] = arguments[_i]; - } -} // Error, can't have initializer -function a9(_a) { - var a = _a[0], b = _a[1], c = _a[2][0][0]; +function c1(_a) { + var z = (_a === void 0 ? { z: 10 } : _a).z; } -function a10(_a) { - var a = _a[0], b = _a[1], c = _a[2][0][0], x = _a.slice(3); +function c2(_a) { + var _b = _a.z, z = _b === void 0 ? 10 : _b; } -function a11(_a) { - var a = _a[0], b = _a[1], c = _a[2], x = _a.slice(3); +function c3(_a) { + var b = (_a === void 0 ? { b: "hello" } : _a).b; } -a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] -var array = [1, 2, 3]; -var array2 = [true, false, "hello"]; -a2(array); -a1.apply(void 0, array); -a1.apply(void 0, array2); // Error parameter type is (number|string)[] -a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] -a9([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] -a9([1, 2]); // Error, parameter type is [any, any, [[any]]] -a10([1, 2, [["string"]], false, true]); // Parameter type is any[] -a10([1, 2, 3, false, true]); // Parameter type is any[] -a10([1, 2]); // Parameter type is any[] -a11([1, 2]); // Parameter type is number[] -a11([1, 2, "string"]); // Error, parameter type is number[] -var C = (function () { - function C(public) { - var a = []; - for (var _i = 1; _i < arguments.length; _i++) { - a[_i - 1] = arguments[_i]; - } - } // Error, rest parameter can't have accessibilityModifier - return C; -})(); -// Rest parameter with generic -function foo() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +function c4(_a, z) { + var z = _a[0]; +} // Error Duplicate identifier +function c5(_a) { + var a = _a[0], b = _a[1], c = _a[2][0][0]; } -foo("hello", 1, 2); // Error -foo("hello", 1, 2); -foo("hello", "world"); -var E; -(function (E) { - E[E["a"] = 0] = "a"; - E[E["b"] = 1] = "b"; -})(E || (E = {})); -function foo1() { - var a = []; - for (var _i = 0; _i < arguments.length; _i++) { - a[_i - 0] = arguments[_i]; - } +function c6(_a) { + var a = _a[0], b = _a[1], _b = _a[2][0][0], c = _b === void 0 ? 1 : _b; } -foo1(1, 2, 3, E.a); -foo1(1, 2, 3, 0 /* a */, E.b); -foo1(1, 2, "string", 0 /* a */, E.b); // Error +c0({ z: 1 }); // Error, implied type is { z: {x: any, y: {j: any}} } +c1({}); // Error, implied type is {z:number}? +c1({ z: true }); // Error, implied type is {z:number}? +c2({ z: false }); // Error, implied type is {z?: number} +c3({ b: true }); // Error, implied type is { b: number|string }. +c5([1, 2, false, true]); // Error, implied type is [any, any, [[any]]] +c6([1, 2, [["string"]]]); // Error, implied type is [any, any, [[number]]] // Use initializer +// A parameter can be marked optional by following its name or binding pattern with a question mark (?) +// or by including an initializer. Initializers (including binding property or element initializers) are +// permitted only when the parameter list occurs in conjunction with a function body +function d1(_a) { + var a = _a[0], b = _a[1], c = _a[2]; +} // Error, binding pattern can't be optional in implementation signature +function d2(_a) { + var x = _a.x, y = _a.y, z = _a.z; +} // Error, binding pattern can't be optional in implementation signature +var C4 = (function () { + function C4() { + } + C4.prototype.d3 = function (_a) { + var a = _a[0], b = _a[1], c = _a[2]; + }; // Error, binding pattern can't be optional in implementation signature + C4.prototype.d4 = function (_a) { + var x = _a.x, y = _a.y, c = _a.c; + }; + C4.prototype.e0 = function (_a) { + var a = _a[0], b = _a[1], q = _a[2]; + }; + return C4; +})(); +// Destructuring parameter declarations do not permit type annotations on the individual binding patterns, +// as such annotations would conflict with the already established meaning of colons in object literals. +// Type annotations must instead be written on the top- level parameter declaration +function e0(_a) { + var _b = _a.x, number = _b[0], number = _b[1], number = _b[2]; +} // should be an error, duplicate identifier; diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.js b/tests/baselines/reference/destructuringParameterDeclaration3ES5.js similarity index 91% rename from tests/baselines/reference/destructuringParameterDeclaration2ES6.js rename to tests/baselines/reference/destructuringParameterDeclaration3ES5.js index 903384c7b4216..5dea671ee1a73 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2ES6.js +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES5.js @@ -1,4 +1,4 @@ -//// [destructuringParameterDeclaration2ES6.ts] +//// [destructuringParameterDeclaration3ES5.ts] // If the parameter is a rest parameter, the parameter type is any[] // A type annotation for a rest parameter must denote an array type. @@ -30,8 +30,7 @@ a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]] a10([1, 2, [["string"]], false, true]); // Parameter type is any[] a10([1, 2, 3, false, true]); // Parameter type is any[] a10([1, 2]); // Parameter type is any[] - -a11([1, 2]); // Parameter type is number[] +a11([1, 2]); // Parameter type is number[] // Rest parameter with generic function foo(...a: T[]) { } @@ -47,7 +46,7 @@ foo1(1, 2, 3, E1.a, E.b); -//// [destructuringParameterDeclaration2ES6.js] +//// [destructuringParameterDeclaration3ES5.js] // If the parameter is a rest parameter, the parameter type is any[] // A type annotation for a rest parameter must denote an array type. function a1(...x) { } diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES5.symbols b/tests/baselines/reference/destructuringParameterDeclaration3ES5.symbols new file mode 100644 index 0000000000000..066be7f9e788f --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES5.symbols @@ -0,0 +1,145 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts === + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +>arrayString : Symbol(arrayString, Decl(destructuringParameterDeclaration3ES5.ts, 0, 0)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) + +type someArray = Array | number[]; +>someArray : Symbol(someArray, Decl(destructuringParameterDeclaration3ES5.ts, 7, 32)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) + +type stringOrNumArray = Array; +>stringOrNumArray : Symbol(stringOrNumArray, Decl(destructuringParameterDeclaration3ES5.ts, 8, 42)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) +>Number : Symbol(Number, Decl(lib.d.ts, 456, 40), Decl(lib.d.ts, 518, 11)) + +function a1(...x: (number|string)[]) { } +>a1 : Symbol(a1, Decl(destructuringParameterDeclaration3ES5.ts, 9, 45)) +>x : Symbol(x, Decl(destructuringParameterDeclaration3ES5.ts, 11, 12)) + +function a2(...a) { } +>a2 : Symbol(a2, Decl(destructuringParameterDeclaration3ES5.ts, 11, 40)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 12, 12)) + +function a3(...a: Array) { } +>a3 : Symbol(a3, Decl(destructuringParameterDeclaration3ES5.ts, 12, 21)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 13, 12)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) + +function a4(...a: arrayString) { } +>a4 : Symbol(a4, Decl(destructuringParameterDeclaration3ES5.ts, 13, 36)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 14, 12)) +>arrayString : Symbol(arrayString, Decl(destructuringParameterDeclaration3ES5.ts, 0, 0)) + +function a5(...a: stringOrNumArray) { } +>a5 : Symbol(a5, Decl(destructuringParameterDeclaration3ES5.ts, 14, 34)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 15, 12)) +>stringOrNumArray : Symbol(stringOrNumArray, Decl(destructuringParameterDeclaration3ES5.ts, 8, 42)) + +function a9([a, b, [[c]]]) { } +>a9 : Symbol(a9, Decl(destructuringParameterDeclaration3ES5.ts, 15, 39)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 16, 13)) +>b : Symbol(b, Decl(destructuringParameterDeclaration3ES5.ts, 16, 15)) +>c : Symbol(c, Decl(destructuringParameterDeclaration3ES5.ts, 16, 21)) + +function a10([a, b, [[c]], ...x]) { } +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES5.ts, 16, 30)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 17, 14)) +>b : Symbol(b, Decl(destructuringParameterDeclaration3ES5.ts, 17, 16)) +>c : Symbol(c, Decl(destructuringParameterDeclaration3ES5.ts, 17, 22)) +>x : Symbol(x, Decl(destructuringParameterDeclaration3ES5.ts, 17, 26)) + +function a11([a, b, c, ...x]: number[]) { } +>a11 : Symbol(a11, Decl(destructuringParameterDeclaration3ES5.ts, 17, 37)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 18, 14)) +>b : Symbol(b, Decl(destructuringParameterDeclaration3ES5.ts, 18, 16)) +>c : Symbol(c, Decl(destructuringParameterDeclaration3ES5.ts, 18, 19)) +>x : Symbol(x, Decl(destructuringParameterDeclaration3ES5.ts, 18, 22)) + + +var array = [1, 2, 3]; +>array : Symbol(array, Decl(destructuringParameterDeclaration3ES5.ts, 21, 3)) + +var array2 = [true, false, "hello"]; +>array2 : Symbol(array2, Decl(destructuringParameterDeclaration3ES5.ts, 22, 3)) + +a2([...array]); +>a2 : Symbol(a2, Decl(destructuringParameterDeclaration3ES5.ts, 11, 40)) +>array : Symbol(array, Decl(destructuringParameterDeclaration3ES5.ts, 21, 3)) + +a1(...array); +>a1 : Symbol(a1, Decl(destructuringParameterDeclaration3ES5.ts, 9, 45)) +>array : Symbol(array, Decl(destructuringParameterDeclaration3ES5.ts, 21, 3)) + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +>a9 : Symbol(a9, Decl(destructuringParameterDeclaration3ES5.ts, 15, 39)) + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES5.ts, 16, 30)) + +a10([1, 2, 3, false, true]); // Parameter type is any[] +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES5.ts, 16, 30)) + +a10([1, 2]); // Parameter type is any[] +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES5.ts, 16, 30)) + +a11([1, 2]); // Parameter type is number[] +>a11 : Symbol(a11, Decl(destructuringParameterDeclaration3ES5.ts, 17, 37)) + +// Rest parameter with generic +function foo(...a: T[]) { } +>foo : Symbol(foo, Decl(destructuringParameterDeclaration3ES5.ts, 31, 12)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES5.ts, 34, 13)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 34, 16)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES5.ts, 34, 13)) + +foo("hello", 1, 2); +>foo : Symbol(foo, Decl(destructuringParameterDeclaration3ES5.ts, 31, 12)) + +foo("hello", "world"); +>foo : Symbol(foo, Decl(destructuringParameterDeclaration3ES5.ts, 31, 12)) + +enum E { a, b } +>E : Symbol(E, Decl(destructuringParameterDeclaration3ES5.ts, 36, 22)) +>a : Symbol(E.a, Decl(destructuringParameterDeclaration3ES5.ts, 38, 8)) +>b : Symbol(E.b, Decl(destructuringParameterDeclaration3ES5.ts, 38, 11)) + +const enum E1 { a, b } +>E1 : Symbol(E1, Decl(destructuringParameterDeclaration3ES5.ts, 38, 15)) +>a : Symbol(E1.a, Decl(destructuringParameterDeclaration3ES5.ts, 39, 15)) +>b : Symbol(E1.b, Decl(destructuringParameterDeclaration3ES5.ts, 39, 18)) + +function foo1(...a: T[]) { } +>foo1 : Symbol(foo1, Decl(destructuringParameterDeclaration3ES5.ts, 39, 22)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES5.ts, 40, 14)) +>Number : Symbol(Number, Decl(lib.d.ts, 456, 40), Decl(lib.d.ts, 518, 11)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES5.ts, 40, 32)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES5.ts, 40, 14)) + +foo1(1, 2, 3, E.a); +>foo1 : Symbol(foo1, Decl(destructuringParameterDeclaration3ES5.ts, 39, 22)) +>E.a : Symbol(E.a, Decl(destructuringParameterDeclaration3ES5.ts, 38, 8)) +>E : Symbol(E, Decl(destructuringParameterDeclaration3ES5.ts, 36, 22)) +>a : Symbol(E.a, Decl(destructuringParameterDeclaration3ES5.ts, 38, 8)) + +foo1(1, 2, 3, E1.a, E.b); +>foo1 : Symbol(foo1, Decl(destructuringParameterDeclaration3ES5.ts, 39, 22)) +>E1.a : Symbol(E1.a, Decl(destructuringParameterDeclaration3ES5.ts, 39, 15)) +>E1 : Symbol(E1, Decl(destructuringParameterDeclaration3ES5.ts, 38, 15)) +>a : Symbol(E1.a, Decl(destructuringParameterDeclaration3ES5.ts, 39, 15)) +>E.b : Symbol(E.b, Decl(destructuringParameterDeclaration3ES5.ts, 38, 11)) +>E : Symbol(E, Decl(destructuringParameterDeclaration3ES5.ts, 36, 22)) +>b : Symbol(E.b, Decl(destructuringParameterDeclaration3ES5.ts, 38, 11)) + + + diff --git a/tests/baselines/reference/destructuringParameterDeclaration2ES6.types b/tests/baselines/reference/destructuringParameterDeclaration3ES5.types similarity index 82% rename from tests/baselines/reference/destructuringParameterDeclaration2ES6.types rename to tests/baselines/reference/destructuringParameterDeclaration3ES5.types index 6215728fefb32..64bede3e2de55 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration2ES6.types +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES5.types @@ -1,4 +1,4 @@ -=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration2ES6.ts === +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES5.ts === // If the parameter is a rest parameter, the parameter type is any[] // A type annotation for a rest parameter must denote an array type. @@ -70,10 +70,16 @@ function a11([a, b, c, ...x]: number[]) { } var array = [1, 2, 3]; >array : number[] >[1, 2, 3] : number[] +>1 : number +>2 : number +>3 : number var array2 = [true, false, "hello"]; >array2 : (string | boolean)[] >[true, false, "hello"] : (string | boolean)[] +>true : boolean +>false : boolean +>"hello" : string a2([...array]); >a2([...array]) : void @@ -92,30 +98,49 @@ a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]] >a9([1, 2, [["string"]], false, true]) : void >a9 : ([a, b, [[c]]]: [any, any, [[any]]]) => void >[1, 2, [["string"]], false, true] : [number, number, [[string]], boolean, boolean] +>1 : number +>2 : number >[["string"]] : [[string]] >["string"] : [string] +>"string" : string +>false : boolean +>true : boolean a10([1, 2, [["string"]], false, true]); // Parameter type is any[] >a10([1, 2, [["string"]], false, true]) : void >a10 : ([a, b, [[c]], ...x]: Iterable) => void >[1, 2, [["string"]], false, true] : (number | boolean | string[][])[] +>1 : number +>2 : number >[["string"]] : string[][] >["string"] : string[] +>"string" : string +>false : boolean +>true : boolean a10([1, 2, 3, false, true]); // Parameter type is any[] >a10([1, 2, 3, false, true]) : void >a10 : ([a, b, [[c]], ...x]: Iterable) => void >[1, 2, 3, false, true] : (number | boolean)[] +>1 : number +>2 : number +>3 : number +>false : boolean +>true : boolean a10([1, 2]); // Parameter type is any[] >a10([1, 2]) : void >a10 : ([a, b, [[c]], ...x]: Iterable) => void >[1, 2] : number[] +>1 : number +>2 : number -a11([1, 2]); // Parameter type is number[] +a11([1, 2]); // Parameter type is number[] >a11([1, 2]) : void >a11 : ([a, b, c, ...x]: number[]) => void >[1, 2] : number[] +>1 : number +>2 : number // Rest parameter with generic function foo(...a: T[]) { } @@ -127,10 +152,15 @@ function foo(...a: T[]) { } foo("hello", 1, 2); >foo("hello", 1, 2) : void >foo : (...a: T[]) => void +>"hello" : string +>1 : number +>2 : number foo("hello", "world"); >foo("hello", "world") : void >foo : (...a: T[]) => void +>"hello" : string +>"world" : string enum E { a, b } >E : E @@ -152,6 +182,9 @@ function foo1(...a: T[]) { } foo1(1, 2, 3, E.a); >foo1(1, 2, 3, E.a) : void >foo1 : (...a: T[]) => void +>1 : number +>2 : number +>3 : number >E.a : E >E : typeof E >a : E @@ -159,6 +192,9 @@ foo1(1, 2, 3, E.a); foo1(1, 2, 3, E1.a, E.b); >foo1(1, 2, 3, E1.a, E.b) : void >foo1 : (...a: T[]) => void +>1 : number +>2 : number +>3 : number >E1.a : E1 >E1 : typeof E1 >a : E1 diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES6.js b/tests/baselines/reference/destructuringParameterDeclaration3ES6.js new file mode 100644 index 0000000000000..5893168bf9335 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES6.js @@ -0,0 +1,80 @@ +//// [destructuringParameterDeclaration3ES6.ts] + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; + +function a1(...x: (number|string)[]) { } +function a2(...a) { } +function a3(...a: Array) { } +function a4(...a: arrayString) { } +function a5(...a: stringOrNumArray) { } +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]: number[]) { } + + +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] +a11([1, 2]); // Parameter type is number[] + +// Rest parameter with generic +function foo(...a: T[]) { } +foo("hello", 1, 2); +foo("hello", "world"); + +enum E { a, b } +const enum E1 { a, b } +function foo1(...a: T[]) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, E1.a, E.b); + + + + +//// [destructuringParameterDeclaration3ES6.js] +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. +function a1(...x) { } +function a2(...a) { } +function a3(...a) { } +function a4(...a) { } +function a5(...a) { } +function a9([a, b, [[c]]]) { } +function a10([a, b, [[c]], ...x]) { } +function a11([a, b, c, ...x]) { } +var array = [1, 2, 3]; +var array2 = [true, false, "hello"]; +a2([...array]); +a1(...array); +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +a10([1, 2, 3, false, true]); // Parameter type is any[] +a10([1, 2]); // Parameter type is any[] +a11([1, 2]); // Parameter type is number[] +// Rest parameter with generic +function foo(...a) { } +foo("hello", 1, 2); +foo("hello", "world"); +var E; +(function (E) { + E[E["a"] = 0] = "a"; + E[E["b"] = 1] = "b"; +})(E || (E = {})); +function foo1(...a) { } +foo1(1, 2, 3, E.a); +foo1(1, 2, 3, 0 /* a */, E.b); diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES6.symbols b/tests/baselines/reference/destructuringParameterDeclaration3ES6.symbols new file mode 100644 index 0000000000000..8e93a257033dc --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES6.symbols @@ -0,0 +1,145 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts === + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +>arrayString : Symbol(arrayString, Decl(destructuringParameterDeclaration3ES6.ts, 0, 0)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) + +type someArray = Array | number[]; +>someArray : Symbol(someArray, Decl(destructuringParameterDeclaration3ES6.ts, 7, 32)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) + +type stringOrNumArray = Array; +>stringOrNumArray : Symbol(stringOrNumArray, Decl(destructuringParameterDeclaration3ES6.ts, 8, 42)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) +>Number : Symbol(Number, Decl(lib.d.ts, 456, 40), Decl(lib.d.ts, 518, 11)) + +function a1(...x: (number|string)[]) { } +>a1 : Symbol(a1, Decl(destructuringParameterDeclaration3ES6.ts, 9, 45)) +>x : Symbol(x, Decl(destructuringParameterDeclaration3ES6.ts, 11, 12)) + +function a2(...a) { } +>a2 : Symbol(a2, Decl(destructuringParameterDeclaration3ES6.ts, 11, 40)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 12, 12)) + +function a3(...a: Array) { } +>a3 : Symbol(a3, Decl(destructuringParameterDeclaration3ES6.ts, 12, 21)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 13, 12)) +>Array : Symbol(Array, Decl(lib.d.ts, 1000, 23), Decl(lib.d.ts, 1171, 11), Decl(lib.d.ts, 1409, 1)) +>String : Symbol(String, Decl(lib.d.ts, 275, 1), Decl(lib.d.ts, 443, 11), Decl(lib.d.ts, 1508, 1)) + +function a4(...a: arrayString) { } +>a4 : Symbol(a4, Decl(destructuringParameterDeclaration3ES6.ts, 13, 36)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 14, 12)) +>arrayString : Symbol(arrayString, Decl(destructuringParameterDeclaration3ES6.ts, 0, 0)) + +function a5(...a: stringOrNumArray) { } +>a5 : Symbol(a5, Decl(destructuringParameterDeclaration3ES6.ts, 14, 34)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 15, 12)) +>stringOrNumArray : Symbol(stringOrNumArray, Decl(destructuringParameterDeclaration3ES6.ts, 8, 42)) + +function a9([a, b, [[c]]]) { } +>a9 : Symbol(a9, Decl(destructuringParameterDeclaration3ES6.ts, 15, 39)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 16, 13)) +>b : Symbol(b, Decl(destructuringParameterDeclaration3ES6.ts, 16, 15)) +>c : Symbol(c, Decl(destructuringParameterDeclaration3ES6.ts, 16, 21)) + +function a10([a, b, [[c]], ...x]) { } +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES6.ts, 16, 30)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 17, 14)) +>b : Symbol(b, Decl(destructuringParameterDeclaration3ES6.ts, 17, 16)) +>c : Symbol(c, Decl(destructuringParameterDeclaration3ES6.ts, 17, 22)) +>x : Symbol(x, Decl(destructuringParameterDeclaration3ES6.ts, 17, 26)) + +function a11([a, b, c, ...x]: number[]) { } +>a11 : Symbol(a11, Decl(destructuringParameterDeclaration3ES6.ts, 17, 37)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 18, 14)) +>b : Symbol(b, Decl(destructuringParameterDeclaration3ES6.ts, 18, 16)) +>c : Symbol(c, Decl(destructuringParameterDeclaration3ES6.ts, 18, 19)) +>x : Symbol(x, Decl(destructuringParameterDeclaration3ES6.ts, 18, 22)) + + +var array = [1, 2, 3]; +>array : Symbol(array, Decl(destructuringParameterDeclaration3ES6.ts, 21, 3)) + +var array2 = [true, false, "hello"]; +>array2 : Symbol(array2, Decl(destructuringParameterDeclaration3ES6.ts, 22, 3)) + +a2([...array]); +>a2 : Symbol(a2, Decl(destructuringParameterDeclaration3ES6.ts, 11, 40)) +>array : Symbol(array, Decl(destructuringParameterDeclaration3ES6.ts, 21, 3)) + +a1(...array); +>a1 : Symbol(a1, Decl(destructuringParameterDeclaration3ES6.ts, 9, 45)) +>array : Symbol(array, Decl(destructuringParameterDeclaration3ES6.ts, 21, 3)) + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +>a9 : Symbol(a9, Decl(destructuringParameterDeclaration3ES6.ts, 15, 39)) + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES6.ts, 16, 30)) + +a10([1, 2, 3, false, true]); // Parameter type is any[] +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES6.ts, 16, 30)) + +a10([1, 2]); // Parameter type is any[] +>a10 : Symbol(a10, Decl(destructuringParameterDeclaration3ES6.ts, 16, 30)) + +a11([1, 2]); // Parameter type is number[] +>a11 : Symbol(a11, Decl(destructuringParameterDeclaration3ES6.ts, 17, 37)) + +// Rest parameter with generic +function foo(...a: T[]) { } +>foo : Symbol(foo, Decl(destructuringParameterDeclaration3ES6.ts, 31, 12)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES6.ts, 34, 13)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 34, 16)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES6.ts, 34, 13)) + +foo("hello", 1, 2); +>foo : Symbol(foo, Decl(destructuringParameterDeclaration3ES6.ts, 31, 12)) + +foo("hello", "world"); +>foo : Symbol(foo, Decl(destructuringParameterDeclaration3ES6.ts, 31, 12)) + +enum E { a, b } +>E : Symbol(E, Decl(destructuringParameterDeclaration3ES6.ts, 36, 22)) +>a : Symbol(E.a, Decl(destructuringParameterDeclaration3ES6.ts, 38, 8)) +>b : Symbol(E.b, Decl(destructuringParameterDeclaration3ES6.ts, 38, 11)) + +const enum E1 { a, b } +>E1 : Symbol(E1, Decl(destructuringParameterDeclaration3ES6.ts, 38, 15)) +>a : Symbol(E1.a, Decl(destructuringParameterDeclaration3ES6.ts, 39, 15)) +>b : Symbol(E1.b, Decl(destructuringParameterDeclaration3ES6.ts, 39, 18)) + +function foo1(...a: T[]) { } +>foo1 : Symbol(foo1, Decl(destructuringParameterDeclaration3ES6.ts, 39, 22)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES6.ts, 40, 14)) +>Number : Symbol(Number, Decl(lib.d.ts, 456, 40), Decl(lib.d.ts, 518, 11)) +>a : Symbol(a, Decl(destructuringParameterDeclaration3ES6.ts, 40, 32)) +>T : Symbol(T, Decl(destructuringParameterDeclaration3ES6.ts, 40, 14)) + +foo1(1, 2, 3, E.a); +>foo1 : Symbol(foo1, Decl(destructuringParameterDeclaration3ES6.ts, 39, 22)) +>E.a : Symbol(E.a, Decl(destructuringParameterDeclaration3ES6.ts, 38, 8)) +>E : Symbol(E, Decl(destructuringParameterDeclaration3ES6.ts, 36, 22)) +>a : Symbol(E.a, Decl(destructuringParameterDeclaration3ES6.ts, 38, 8)) + +foo1(1, 2, 3, E1.a, E.b); +>foo1 : Symbol(foo1, Decl(destructuringParameterDeclaration3ES6.ts, 39, 22)) +>E1.a : Symbol(E1.a, Decl(destructuringParameterDeclaration3ES6.ts, 39, 15)) +>E1 : Symbol(E1, Decl(destructuringParameterDeclaration3ES6.ts, 38, 15)) +>a : Symbol(E1.a, Decl(destructuringParameterDeclaration3ES6.ts, 39, 15)) +>E.b : Symbol(E.b, Decl(destructuringParameterDeclaration3ES6.ts, 38, 11)) +>E : Symbol(E, Decl(destructuringParameterDeclaration3ES6.ts, 36, 22)) +>b : Symbol(E.b, Decl(destructuringParameterDeclaration3ES6.ts, 38, 11)) + + + diff --git a/tests/baselines/reference/destructuringParameterDeclaration3ES6.types b/tests/baselines/reference/destructuringParameterDeclaration3ES6.types new file mode 100644 index 0000000000000..aea7b2ae15af0 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration3ES6.types @@ -0,0 +1,206 @@ +=== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3ES6.ts === + +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. + +// RestParameter: +// ... Identifier TypeAnnotation(opt) + +type arrayString = Array +>arrayString : String[] +>Array : T[] +>String : String + +type someArray = Array | number[]; +>someArray : number[] | String[] +>Array : T[] +>String : String + +type stringOrNumArray = Array; +>stringOrNumArray : (String | Number)[] +>Array : T[] +>String : String +>Number : Number + +function a1(...x: (number|string)[]) { } +>a1 : (...x: (string | number)[]) => void +>x : (string | number)[] + +function a2(...a) { } +>a2 : (...a: any[]) => void +>a : any[] + +function a3(...a: Array) { } +>a3 : (...a: String[]) => void +>a : String[] +>Array : T[] +>String : String + +function a4(...a: arrayString) { } +>a4 : (...a: String[]) => void +>a : String[] +>arrayString : String[] + +function a5(...a: stringOrNumArray) { } +>a5 : (...a: (String | Number)[]) => void +>a : (String | Number)[] +>stringOrNumArray : (String | Number)[] + +function a9([a, b, [[c]]]) { } +>a9 : ([a, b, [[c]]]: [any, any, [[any]]]) => void +>a : any +>b : any +>c : any + +function a10([a, b, [[c]], ...x]) { } +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>a : any +>b : any +>c : any +>x : any[] + +function a11([a, b, c, ...x]: number[]) { } +>a11 : ([a, b, c, ...x]: number[]) => void +>a : number +>b : number +>c : number +>x : number[] + + +var array = [1, 2, 3]; +>array : number[] +>[1, 2, 3] : number[] +>1 : number +>2 : number +>3 : number + +var array2 = [true, false, "hello"]; +>array2 : (string | boolean)[] +>[true, false, "hello"] : (string | boolean)[] +>true : boolean +>false : boolean +>"hello" : string + +a2([...array]); +>a2([...array]) : void +>a2 : (...a: any[]) => void +>[...array] : number[] +>...array : number +>array : number[] + +a1(...array); +>a1(...array) : void +>a1 : (...x: (string | number)[]) => void +>...array : number +>array : number[] + +a9([1, 2, [["string"]], false, true]); // Parameter type is [any, any, [[any]]] +>a9([1, 2, [["string"]], false, true]) : void +>a9 : ([a, b, [[c]]]: [any, any, [[any]]]) => void +>[1, 2, [["string"]], false, true] : [number, number, [[string]], boolean, boolean] +>1 : number +>2 : number +>[["string"]] : [[string]] +>["string"] : [string] +>"string" : string +>false : boolean +>true : boolean + +a10([1, 2, [["string"]], false, true]); // Parameter type is any[] +>a10([1, 2, [["string"]], false, true]) : void +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>[1, 2, [["string"]], false, true] : (number | boolean | string[][])[] +>1 : number +>2 : number +>[["string"]] : string[][] +>["string"] : string[] +>"string" : string +>false : boolean +>true : boolean + +a10([1, 2, 3, false, true]); // Parameter type is any[] +>a10([1, 2, 3, false, true]) : void +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>[1, 2, 3, false, true] : (number | boolean)[] +>1 : number +>2 : number +>3 : number +>false : boolean +>true : boolean + +a10([1, 2]); // Parameter type is any[] +>a10([1, 2]) : void +>a10 : ([a, b, [[c]], ...x]: Iterable) => void +>[1, 2] : number[] +>1 : number +>2 : number + +a11([1, 2]); // Parameter type is number[] +>a11([1, 2]) : void +>a11 : ([a, b, c, ...x]: number[]) => void +>[1, 2] : number[] +>1 : number +>2 : number + +// Rest parameter with generic +function foo(...a: T[]) { } +>foo : (...a: T[]) => void +>T : T +>a : T[] +>T : T + +foo("hello", 1, 2); +>foo("hello", 1, 2) : void +>foo : (...a: T[]) => void +>"hello" : string +>1 : number +>2 : number + +foo("hello", "world"); +>foo("hello", "world") : void +>foo : (...a: T[]) => void +>"hello" : string +>"world" : string + +enum E { a, b } +>E : E +>a : E +>b : E + +const enum E1 { a, b } +>E1 : E1 +>a : E1 +>b : E1 + +function foo1(...a: T[]) { } +>foo1 : (...a: T[]) => void +>T : T +>Number : Number +>a : T[] +>T : T + +foo1(1, 2, 3, E.a); +>foo1(1, 2, 3, E.a) : void +>foo1 : (...a: T[]) => void +>1 : number +>2 : number +>3 : number +>E.a : E +>E : typeof E +>a : E + +foo1(1, 2, 3, E1.a, E.b); +>foo1(1, 2, 3, E1.a, E.b) : void +>foo1 : (...a: T[]) => void +>1 : number +>2 : number +>3 : number +>E1.a : E1 +>E1 : typeof E1 +>a : E1 +>E.b : E +>E : typeof E +>b : E + + + diff --git a/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt index d5d0888e632cb..707fb005a8045 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration4.errors.txt @@ -1,78 +1,84 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(46,4): error TS2345: Argument of type '{ y: Class; }' is not assignable to parameter of type '{ y: D; }'. - Types of property 'y' are incompatible. - Type 'Class' is not assignable to type 'D'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(47,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ y: D; }'. - Property 'y' is missing in type '{}'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(48,4): error TS2345: Argument of type '{ y: number; }' is not assignable to parameter of type '{ y: D; }'. - Types of property 'y' are incompatible. - Type 'number' is not assignable to type 'D'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(49,4): error TS2345: Argument of type '{ y: string; }' is not assignable to parameter of type '{ y: D; }'. - Types of property 'y' are incompatible. - Type 'string' is not assignable to type 'D'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(11,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(13,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(20,19): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. + Type 'boolean' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(21,7): error TS2304: Cannot find name 'array2'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(22,4): error TS2345: Argument of type '[number, number, string, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. + Types of property '2' are incompatible. + Type 'string' is not assignable to type '[[any]]'. + Property '0' is missing in type 'String'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(23,4): error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]]]'. + Property '2' is missing in type '[number, number]'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(24,4): error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'. + Type 'string | number' is not assignable to type 'number'. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(29,24): error TS1005: ',' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(34,22): error TS2304: Cannot find name 'E1'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(34,28): error TS2304: Cannot find name 'E'. -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts (4 errors) ==== - // Parameter with generic - interface F { } - class Class implements F { - constructor() { } - } +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts (10 errors) ==== + // If the parameter is a rest parameter, the parameter type is any[] + // A type annotation for a rest parameter must denote an array type. - class SubClass extends Class { - foo: boolean; - constructor() { super(); } - } + // RestParameter: + // ... Identifier TypeAnnotation(opt) - class D implements F { - foo: boolean - constructor() { } - } + type arrayString = Array + type someArray = Array | number[]; + type stringOrNumArray = Array; + + function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2370: A rest parameter must be of an array type. + function a1(...x: (number|string)[]) { } + function a2(...a: someArray) { } // Error, rest parameter must be array type + ~~~~~~~~~~~~~~~ +!!! error TS2370: A rest parameter must be of an array type. + function a3(...b?) { } // Error, can't be optional + function a4(...b = [1,2,3]) { } // Error, can't have initializer + function a5([a, b, [[c]]]) { } + function a6([a, b, c, ...x]: number[]) { } - class SubD extends D { - bar: number - constructor() { - super(); - } - } + a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] + ~~~~ +!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string | number'. +!!! error TS2345: Type 'boolean' is not assignable to type 'number'. + a1(...array2); // Error parameter type is (number|string)[] + ~~~~~~ +!!! error TS2304: Cannot find name 'array2'. + a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '[number, number, string, boolean, boolean]' is not assignable to parameter of type '[any, any, [[any]]]'. +!!! error TS2345: Types of property '2' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type '[[any]]'. +!!! error TS2345: Property '0' is missing in type 'String'. + a5([1, 2]); // Error, parameter type is [any, any, [[any]]] + ~~~~~~ +!!! error TS2345: Argument of type '[number, number]' is not assignable to parameter of type '[any, any, [[any]]]'. +!!! error TS2345: Property '2' is missing in type '[number, number]'. + a6([1, 2, "string"]); // Error, parameter type is number[] + ~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'. +!!! error TS2345: Type 'string | number' is not assignable to type 'number'. +!!! error TS2345: Type 'string' is not assignable to type 'number'. - function d0({x} = { x: new Class() }) { } - function d1({x}: { x: F }) { } - function d2({x}: { x: Class }) { } - function d3({y}: { y: D }) { } - function d4({y} = { y: new D() }) { } - var obj = new Class(); - d0({ x: 1 }); - d0({ x: {} }); - d0({ x: "string" }); + var temp = [1, 2, 3]; + class C { + constructor(public ...temp) { } // Error, rest parameter can't have accessibilityModifier + ~~~ +!!! error TS1005: ',' expected. + } - d1({ x: new Class() }); - d1({ x: {} }); - d1({ x: "string" }); + // Rest parameter with generic + function foo1(...a: T[]) { } + foo1(1, 2, "string", E1.a, E.b); // Error + ~~ +!!! error TS2304: Cannot find name 'E1'. + ~ +!!! error TS2304: Cannot find name 'E'. - d2({ x: new SubClass() }); - d2({ x: {} }); - d3({ y: new SubD() }); - d3({ y: new SubClass() }); - // Error - d3({ y: new Class() }); - ~~~~~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '{ y: Class; }' is not assignable to parameter of type '{ y: D; }'. -!!! error TS2345: Types of property 'y' are incompatible. -!!! error TS2345: Type 'Class' is not assignable to type 'D'. - d3({}); - ~~ -!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ y: D; }'. -!!! error TS2345: Property 'y' is missing in type '{}'. - d3({ y: 1 }); - ~~~~~~~~ -!!! error TS2345: Argument of type '{ y: number; }' is not assignable to parameter of type '{ y: D; }'. -!!! error TS2345: Types of property 'y' are incompatible. -!!! error TS2345: Type 'number' is not assignable to type 'D'. - d3({ y: "world" }); - ~~~~~~~~~~~~~~ -!!! error TS2345: Argument of type '{ y: string; }' is not assignable to parameter of type '{ y: D; }'. -!!! error TS2345: Types of property 'y' are incompatible. -!!! error TS2345: Type 'string' is not assignable to type 'D'. \ No newline at end of file + \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration4.js b/tests/baselines/reference/destructuringParameterDeclaration4.js index 3eb0b3f8c1930..adc5264e1ffeb 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration4.js +++ b/tests/baselines/reference/destructuringParameterDeclaration4.js @@ -1,113 +1,101 @@ //// [destructuringParameterDeclaration4.ts] -// Parameter with generic -interface F { } -class Class implements F { - constructor() { } -} +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. -class SubClass extends Class { - foo: boolean; - constructor() { super(); } -} +// RestParameter: +// ... Identifier TypeAnnotation(opt) -class D implements F { - foo: boolean - constructor() { } -} +type arrayString = Array +type someArray = Array | number[]; +type stringOrNumArray = Array; -class SubD extends D { - bar: number - constructor() { - super(); - } -} +function a0(...x: [number, number, string]) { } // Error, rest parameter must be array type +function a1(...x: (number|string)[]) { } +function a2(...a: someArray) { } // Error, rest parameter must be array type +function a3(...b?) { } // Error, can't be optional +function a4(...b = [1,2,3]) { } // Error, can't have initializer +function a5([a, b, [[c]]]) { } +function a6([a, b, c, ...x]: number[]) { } -function d0({x} = { x: new Class() }) { } -function d1({x}: { x: F }) { } -function d2({x}: { x: Class }) { } -function d3({y}: { y: D }) { } -function d4({y} = { y: new D() }) { } +a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] +a1(...array2); // Error parameter type is (number|string)[] +a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] +a5([1, 2]); // Error, parameter type is [any, any, [[any]]] +a6([1, 2, "string"]); // Error, parameter type is number[] -var obj = new Class(); -d0({ x: 1 }); -d0({ x: {} }); -d0({ x: "string" }); -d1({ x: new Class() }); -d1({ x: {} }); -d1({ x: "string" }); +var temp = [1, 2, 3]; +class C { + constructor(public ...temp) { } // Error, rest parameter can't have accessibilityModifier +} + +// Rest parameter with generic +function foo1(...a: T[]) { } +foo1(1, 2, "string", E1.a, E.b); // Error -d2({ x: new SubClass() }); -d2({ x: {} }); -d3({ y: new SubD() }); -d3({ y: new SubClass() }); -// Error -d3({ y: new Class() }); -d3({}); -d3({ y: 1 }); -d3({ y: "world" }); + //// [destructuringParameterDeclaration4.js] -var __extends = this.__extends || function (d, b) { - for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; - function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); -}; -var Class = (function () { - function Class() { +// If the parameter is a rest parameter, the parameter type is any[] +// A type annotation for a rest parameter must denote an array type. +function a0() { + var x = []; + for (var _i = 0; _i < arguments.length; _i++) { + x[_i - 0] = arguments[_i]; } - return Class; -})(); -var SubClass = (function (_super) { - __extends(SubClass, _super); - function SubClass() { - _super.call(this); +} // Error, rest parameter must be array type +function a1() { + var x = []; + for (var _i = 0; _i < arguments.length; _i++) { + x[_i - 0] = arguments[_i]; } - return SubClass; -})(Class); -var D = (function () { - function D() { +} +function a2() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; } - return D; -})(); -var SubD = (function (_super) { - __extends(SubD, _super); - function SubD() { - _super.call(this); +} // Error, rest parameter must be array type +function a3() { + var b = []; + for (var _i = 0; _i < arguments.length; _i++) { + b[_i - 0] = arguments[_i]; } - return SubD; -})(D); -function d0(_a) { - var x = (_a === void 0 ? { x: new Class() } : _a).x; -} -function d1(_a) { - var x = _a.x; -} -function d2(_a) { - var x = _a.x; +} // Error, can't be optional +function a4() { + var b = []; + for (var _i = 0; _i < arguments.length; _i++) { + b[_i - 0] = arguments[_i]; + } +} // Error, can't have initializer +function a5(_a) { + var a = _a[0], b = _a[1], c = _a[2][0][0]; } -function d3(_a) { - var y = _a.y; +function a6(_a) { + var a = _a[0], b = _a[1], c = _a[2], x = _a.slice(3); } -function d4(_a) { - var y = (_a === void 0 ? { y: new D() } : _a).y; +a1(1, 2, "hello", true); // Error, parameter type is (number|string)[] +a1.apply(void 0, array2); // Error parameter type is (number|string)[] +a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]] +a5([1, 2]); // Error, parameter type is [any, any, [[any]]] +a6([1, 2, "string"]); // Error, parameter type is number[] +var temp = [1, 2, 3]; +var C = (function () { + function C(public) { + var temp = []; + for (var _i = 1; _i < arguments.length; _i++) { + temp[_i - 1] = arguments[_i]; + } + } // Error, rest parameter can't have accessibilityModifier + return C; +})(); +// Rest parameter with generic +function foo1() { + var a = []; + for (var _i = 0; _i < arguments.length; _i++) { + a[_i - 0] = arguments[_i]; + } } -var obj = new Class(); -d0({ x: 1 }); -d0({ x: {} }); -d0({ x: "string" }); -d1({ x: new Class() }); -d1({ x: {} }); -d1({ x: "string" }); -d2({ x: new SubClass() }); -d2({ x: {} }); -d3({ y: new SubD() }); -d3({ y: new SubClass() }); -// Error -d3({ y: new Class() }); -d3({}); -d3({ y: 1 }); -d3({ y: "world" }); +foo1(1, 2, "string", E1.a, E.b); // Error diff --git a/tests/baselines/reference/destructuringParameterDeclaration5.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration5.errors.txt new file mode 100644 index 0000000000000..c60e2b75c3759 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration5.errors.txt @@ -0,0 +1,79 @@ +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts(47,4): error TS2345: Argument of type '{ y: Class; }' is not assignable to parameter of type '{ y: D; }'. + Types of property 'y' are incompatible. + Type 'Class' is not assignable to type 'D'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts(48,4): error TS2345: Argument of type '{}' is not assignable to parameter of type '{ y: D; }'. + Property 'y' is missing in type '{}'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts(49,4): error TS2345: Argument of type '{ y: number; }' is not assignable to parameter of type '{ y: D; }'. + Types of property 'y' are incompatible. + Type 'number' is not assignable to type 'D'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts(50,4): error TS2345: Argument of type '{ y: string; }' is not assignable to parameter of type '{ y: D; }'. + Types of property 'y' are incompatible. + Type 'string' is not assignable to type 'D'. + + +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration5.ts (4 errors) ==== + // Parameter Declaration with generic + + interface F { } + class Class implements F { + constructor() { } + } + + class SubClass extends Class { + foo: boolean; + constructor() { super(); } + } + + class D implements F { + foo: boolean + constructor() { } + } + + class SubD extends D { + bar: number + constructor() { + super(); + } + } + + + function d0({x} = { x: new Class() }) { } + function d1({x}: { x: F }) { } + function d2({x}: { x: Class }) { } + function d3({y}: { y: D }) { } + function d4({y} = { y: new D() }) { } + + var obj = new Class(); + d0({ x: 1 }); + d0({ x: {} }); + d0({ x: "string" }); + + d1({ x: new Class() }); + d1({ x: {} }); + d1({ x: "string" }); + + d2({ x: new SubClass() }); + d2({ x: {} }); + + d3({ y: new SubD() }); + d3({ y: new SubClass() }); + // Error + d3({ y: new Class() }); + ~~~~~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ y: Class; }' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Types of property 'y' are incompatible. +!!! error TS2345: Type 'Class' is not assignable to type 'D'. + d3({}); + ~~ +!!! error TS2345: Argument of type '{}' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Property 'y' is missing in type '{}'. + d3({ y: 1 }); + ~~~~~~~~ +!!! error TS2345: Argument of type '{ y: number; }' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Types of property 'y' are incompatible. +!!! error TS2345: Type 'number' is not assignable to type 'D'. + d3({ y: "world" }); + ~~~~~~~~~~~~~~ +!!! error TS2345: Argument of type '{ y: string; }' is not assignable to parameter of type '{ y: D; }'. +!!! error TS2345: Types of property 'y' are incompatible. +!!! error TS2345: Type 'string' is not assignable to type 'D'. \ No newline at end of file diff --git a/tests/baselines/reference/destructuringParameterDeclaration5.js b/tests/baselines/reference/destructuringParameterDeclaration5.js new file mode 100644 index 0000000000000..2dea8224c65c6 --- /dev/null +++ b/tests/baselines/reference/destructuringParameterDeclaration5.js @@ -0,0 +1,115 @@ +//// [destructuringParameterDeclaration5.ts] +// Parameter Declaration with generic + +interface F { } +class Class implements F { + constructor() { } +} + +class SubClass extends Class { + foo: boolean; + constructor() { super(); } +} + +class D implements F { + foo: boolean + constructor() { } +} + +class SubD extends D { + bar: number + constructor() { + super(); + } +} + + +function d0({x} = { x: new Class() }) { } +function d1({x}: { x: F }) { } +function d2({x}: { x: Class }) { } +function d3({y}: { y: D }) { } +function d4({y} = { y: new D() }) { } + +var obj = new Class(); +d0({ x: 1 }); +d0({ x: {} }); +d0({ x: "string" }); + +d1({ x: new Class() }); +d1({ x: {} }); +d1({ x: "string" }); + +d2({ x: new SubClass() }); +d2({ x: {} }); + +d3({ y: new SubD() }); +d3({ y: new SubClass() }); +// Error +d3({ y: new Class() }); +d3({}); +d3({ y: 1 }); +d3({ y: "world" }); + +//// [destructuringParameterDeclaration5.js] +// Parameter Declaration with generic +var __extends = this.__extends || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + __.prototype = b.prototype; + d.prototype = new __(); +}; +var Class = (function () { + function Class() { + } + return Class; +})(); +var SubClass = (function (_super) { + __extends(SubClass, _super); + function SubClass() { + _super.call(this); + } + return SubClass; +})(Class); +var D = (function () { + function D() { + } + return D; +})(); +var SubD = (function (_super) { + __extends(SubD, _super); + function SubD() { + _super.call(this); + } + return SubD; +})(D); +function d0(_a) { + var x = (_a === void 0 ? { x: new Class() } : _a).x; +} +function d1(_a) { + var x = _a.x; +} +function d2(_a) { + var x = _a.x; +} +function d3(_a) { + var y = _a.y; +} +function d4(_a) { + var y = (_a === void 0 ? { y: new D() } : _a).y; +} +var obj = new Class(); +d0({ x: 1 }); +d0({ x: {} }); +d0({ x: "string" }); +d1({ x: new Class() }); +d1({ x: {} }); +d1({ x: "string" }); +d2({ x: new SubClass() }); +d2({ x: {} }); +d3({ y: new SubD() }); +d3({ y: new SubClass() }); +// Error +d3({ y: new Class() }); +d3({}); +d3({ y: 1 }); +d3({ y: "world" }); diff --git a/tests/baselines/reference/destructuringParameterDeclaration3.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration6.errors.txt similarity index 75% rename from tests/baselines/reference/destructuringParameterDeclaration3.errors.txt rename to tests/baselines/reference/destructuringParameterDeclaration6.errors.txt index 577775827315c..e96f279b7e971 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration3.errors.txt +++ b/tests/baselines/reference/destructuringParameterDeclaration6.errors.txt @@ -1,16 +1,16 @@ -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,14): error TS1181: Array element destructuring pattern expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,19): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,21): error TS1109: Expression expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,24): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,26): error TS2304: Cannot find name 'public'. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,32): error TS1005: ';' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(9,33): error TS1128: Declaration or statement expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(10,16): error TS1003: Identifier expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(10,21): error TS1005: '(' expected. -tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts(12,13): error TS2370: A rest parameter must be of an array type. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,14): error TS1181: Array element destructuring pattern expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,19): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,21): error TS1109: Expression expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,24): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,26): error TS2304: Cannot find name 'public'. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,32): error TS1005: ';' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(9,33): error TS1128: Declaration or statement expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(10,16): error TS1003: Identifier expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(10,21): error TS1005: '(' expected. +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts(12,13): error TS2370: A rest parameter must be of an array type. -==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration3.ts (10 errors) ==== +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration6.ts (10 errors) ==== // A parameter declaration may specify either an identifier or a binding pattern. // Reserved words are not allowed to be used as an identifier in parameter declaration diff --git a/tests/baselines/reference/destructuringParameterDeclaration3.js b/tests/baselines/reference/destructuringParameterDeclaration6.js similarity index 90% rename from tests/baselines/reference/destructuringParameterDeclaration3.js rename to tests/baselines/reference/destructuringParameterDeclaration6.js index a1abb86bd63e8..ff6662c4e8672 100644 --- a/tests/baselines/reference/destructuringParameterDeclaration3.js +++ b/tests/baselines/reference/destructuringParameterDeclaration6.js @@ -1,4 +1,4 @@ -//// [destructuringParameterDeclaration3.ts] +//// [destructuringParameterDeclaration6.ts] // A parameter declaration may specify either an identifier or a binding pattern. // Reserved words are not allowed to be used as an identifier in parameter declaration @@ -21,7 +21,7 @@ b2({ while: 1 }); -//// [destructuringParameterDeclaration3.js] +//// [destructuringParameterDeclaration6.js] // A parameter declaration may specify either an identifier or a binding pattern. // Reserved words are not allowed to be used as an identifier in parameter declaration "use strict";