diff --git a/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt b/tests/baselines/reference/destructuringParameterDeclaration1.errors.txt index cc0e8167d1..078895d07b 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 d896351183..0d8590ae50 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 0000000000..6310a8f638 --- /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 0000000000..ed77f2dc92 --- /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 314c1aafee..60f8560455 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 11bda7049e..187966c359 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 0000000000..903384c7b4 --- /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 0000000000..6215728fef --- /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 027d11b8c2..a36e679751 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 0000000000..58d69729b9 --- /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 5a775eb69c..b764e4f286 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 0000000000..59b08013a2 --- /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); + +