|
| 1 | +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(96,18): error TS2300: Duplicate identifier 'number'. |
| 2 | +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(96,26): error TS2300: Duplicate identifier 'number'. |
| 3 | +tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts(96,34): error TS2300: Duplicate identifier 'number'. |
| 4 | + |
| 5 | + |
| 6 | +==== tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration1ES6.ts (3 errors) ==== |
| 7 | + // Conformance for emitting ES6 |
| 8 | + |
| 9 | + // A parameter declaration may specify either an identifier or a binding pattern. |
| 10 | + // The identifiers specified in parameter declarations and binding patterns |
| 11 | + // in a parameter list must be unique within that parameter list. |
| 12 | + |
| 13 | + // If the declaration includes a type annotation, the parameter is of that type |
| 14 | + function a1([a, b, [[c]]]: [number, number, string[][]]) { } |
| 15 | + function a2(o: { x: number, a: number }) { } |
| 16 | + function a3({j, k, l: {m, n}, q: [a, b, c]}: { j: number, k: string, l: { m: boolean, n: number }, q: (number|string)[] }) { }; |
| 17 | + function a4({x, a}: { x: number, a: number }) { } |
| 18 | + |
| 19 | + a1([1, 2, [["world"]]]); |
| 20 | + a1([1, 2, [["world"]], 3]); |
| 21 | + |
| 22 | + |
| 23 | + // If the declaration includes an initializer expression (which is permitted only |
| 24 | + // when the parameter list occurs in conjunction with a function body), |
| 25 | + // the parameter type is the widened form (section 3.11) of the type of the initializer expression. |
| 26 | + |
| 27 | + function b1(z = [undefined, null]) { }; |
| 28 | + function b2(z = null, o = { x: 0, y: undefined }) { } |
| 29 | + function b3({z: {x, y: {j}}} = { z: { x: "hi", y: { j: 1 } } }) { } |
| 30 | + |
| 31 | + interface F1 { |
| 32 | + b5(z, y, [, a, b], {p, m: { q, r}}); |
| 33 | + } |
| 34 | + |
| 35 | + function b6([a, z, y] = [undefined, null, undefined]) { } |
| 36 | + function b7([[a], b, [[c, d]]] = [[undefined], undefined, [[undefined, undefined]]]) { } |
| 37 | + |
| 38 | + b1([1, 2, 3]); // z is widen to the type any[] |
| 39 | + b2("string", { x: 200, y: "string" }); |
| 40 | + b2("string", { x: 200, y: true }); |
| 41 | + |
| 42 | + |
| 43 | + // If the declaration specifies a binding pattern, the parameter type is the implied type of that binding pattern (section 5.1.3) |
| 44 | + enum Foo { a } |
| 45 | + function c0({z: {x, y: {j}}}) { } |
| 46 | + function c1({z} = { z: 10 }) { } |
| 47 | + function c2({z = 10}) { } |
| 48 | + function c3({b}: { b: number|string} = { b: "hello" }) { } |
| 49 | + function c5([a, b, [[c]]]) { } |
| 50 | + function c6([a, b, [[c=1]]]) { } |
| 51 | + |
| 52 | + c0({z : { x: 1, y: { j: "world" } }}); // Implied type is { z: {x: any, y: {j: any}} } |
| 53 | + c0({z : { x: "string", y: { j: true } }}); // Implied type is { z: {x: any, y: {j: any}} } |
| 54 | + |
| 55 | + c1(); // Implied type is {z:number}? |
| 56 | + c1({ z: 1 }) // Implied type is {z:number}? |
| 57 | + |
| 58 | + c2({}); // Implied type is {z?: number} |
| 59 | + c2({z:1}); // Implied type is {z?: number} |
| 60 | + |
| 61 | + c3({ b: 1 }); // Implied type is { b: number|string }. |
| 62 | + |
| 63 | + c5([1, 2, [["string"]]]); // Implied type is is [any, any, [[any]]] |
| 64 | + c5([1, 2, [["string"]], false, true]); // Implied type is is [any, any, [[any]]] |
| 65 | + |
| 66 | + |
| 67 | + // A parameter can be marked optional by following its name or binding pattern with a question mark (?) |
| 68 | + // or by including an initializer. |
| 69 | + |
| 70 | + interface F2 { |
| 71 | + d3([a, b, c]?); |
| 72 | + d4({x, y, z}?); |
| 73 | + e0([a, b, c]); |
| 74 | + } |
| 75 | + |
| 76 | + class C2 implements F2 { |
| 77 | + constructor() { } |
| 78 | + d3() { } |
| 79 | + d4() { } |
| 80 | + e0([a, b, c]) { } |
| 81 | + } |
| 82 | + |
| 83 | + class C3 implements F2 { |
| 84 | + d3([a, b, c]) { } |
| 85 | + d4({x, y, z}) { } |
| 86 | + e0([a, b, c]) { } |
| 87 | + } |
| 88 | + |
| 89 | + function d5({x, y} = { x: 1, y: 2 }) { } |
| 90 | + d5(); // Parameter is optional as its declaration included an initializer |
| 91 | + |
| 92 | + // Destructuring parameter declarations do not permit type annotations on the individual binding patterns, |
| 93 | + // as such annotations would conflict with the already established meaning of colons in object literals. |
| 94 | + // Type annotations must instead be written on the top- level parameter declaration |
| 95 | + |
| 96 | + function e1({x: number}) { } // x has type any NOT number |
| 97 | + function e2({x}: { x: number }) { } // x is type number |
| 98 | + function e3({x}: { x?: number }) { } // x is an optional with type number |
| 99 | + function e4({x: [number,string,any] }) { } // x has type [any, any, any] |
| 100 | + function e5({x: [a, b, c]}: { x: [number, number, number] }) { } // x has type [any, any, any] |
| 101 | + |
| 102 | + function e6({x: [number, number, number]}) { } // error, duplicate identifier; |
| 103 | + ~~~~~~ |
| 104 | +!!! error TS2300: Duplicate identifier 'number'. |
| 105 | + ~~~~~~ |
| 106 | +!!! error TS2300: Duplicate identifier 'number'. |
| 107 | + ~~~~~~ |
| 108 | +!!! error TS2300: Duplicate identifier 'number'. |
| 109 | + |
| 110 | + |
| 111 | + |
0 commit comments