Skip to content

Commit dcb9e1c

Browse files
committed
Merge pull request #4598 from Microsoft/destructuringInitializers
Improved checking of destructuring with literal initializers
2 parents b71969e + 262f122 commit dcb9e1c

File tree

50 files changed

+1425
-1247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1425
-1247
lines changed

src/compiler/checker.ts

+108-39
Large diffs are not rendered by default.

src/compiler/diagnosticInformationMap.generated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ namespace ts {
415415
The_arguments_object_cannot_be_referenced_in_an_async_arrow_function_Consider_using_a_standard_async_function_expression: { code: 2522, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an async arrow function. Consider using a standard async function expression." },
416416
yield_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2523, category: DiagnosticCategory.Error, key: "'yield' expressions cannot be used in a parameter initializer." },
417417
await_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2524, category: DiagnosticCategory.Error, key: "'await' expressions cannot be used in a parameter initializer." },
418+
Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value: { code: 2525, category: DiagnosticCategory.Error, key: "Initializer provides no value for this binding element and the binding element has no default value." },
418419
JSX_element_attributes_type_0_must_be_an_object_type: { code: 2600, category: DiagnosticCategory.Error, key: "JSX element attributes type '{0}' must be an object type." },
419420
The_return_type_of_a_JSX_element_constructor_must_return_an_object_type: { code: 2601, category: DiagnosticCategory.Error, key: "The return type of a JSX element constructor must return an object type." },
420421
JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist: { code: 2602, category: DiagnosticCategory.Error, key: "JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist." },

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,10 @@
16491649
"category": "Error",
16501650
"code": 2524
16511651
},
1652+
"Initializer provides no value for this binding element and the binding element has no default value.": {
1653+
"category": "Error",
1654+
"code": 2525
1655+
},
16521656
"JSX element attributes type '{0}' must be an object type.": {
16531657
"category": "Error",
16541658
"code": 2600

src/compiler/types.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ namespace ts {
17151715
resolvedExports?: SymbolTable; // Resolved exports of module
17161716
exportsChecked?: boolean; // True if exports of external module have been checked
17171717
isNestedRedeclaration?: boolean; // True if symbol is block scoped redeclaration
1718+
bindingElement?: BindingElement; // Binding element associated with property symbol
17181719
}
17191720

17201721
/* @internal */
@@ -1812,11 +1813,14 @@ namespace ts {
18121813
PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType
18131814
}
18141815

1816+
export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
1817+
18151818
// Properties common to all types
18161819
export interface Type {
1817-
flags: TypeFlags; // Flags
1818-
/* @internal */ id: number; // Unique ID
1819-
symbol?: Symbol; // Symbol associated with type (if any)
1820+
flags: TypeFlags; // Flags
1821+
/* @internal */ id: number; // Unique ID
1822+
symbol?: Symbol; // Symbol associated with type (if any)
1823+
pattern?: DestructuringPattern; // Destructuring pattern represented by type (if any)
18201824
}
18211825

18221826
/* @internal */
@@ -1865,8 +1869,7 @@ namespace ts {
18651869
}
18661870

18671871
export interface TupleType extends ObjectType {
1868-
elementTypes: Type[]; // Element types
1869-
baseArrayType: TypeReference; // Array<T> where T is best common type of element types
1872+
elementTypes: Type[]; // Element types
18701873
}
18711874

18721875
export interface UnionOrIntersectionType extends Type {

tests/baselines/reference/arrowFunctionExpressions.types

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ var p8 = ({ a = 1 }) => { };
100100
>1 : number
101101

102102
var p9 = ({ a: { b = 1 } = { b: 1 } }) => { };
103-
>p9 : ({ a: { b = 1 } = { b: 1 } }: { a?: { b: number; }; }) => void
104-
>({ a: { b = 1 } = { b: 1 } }) => { } : ({ a: { b = 1 } = { b: 1 } }: { a?: { b: number; }; }) => void
103+
>p9 : ({ a: { b = 1 } = { b: 1 } }: { a?: { b?: number; }; }) => void
104+
>({ a: { b = 1 } = { b: 1 } }) => { } : ({ a: { b = 1 } = { b: 1 } }: { a?: { b?: number; }; }) => void
105105
>a : any
106106
>b : number
107107
>1 : number
108-
>{ b: 1 } : { b: number; }
108+
>{ b: 1 } : { b?: number; }
109109
>b : number
110110
>1 : number
111111

tests/baselines/reference/declarationEmitDestructuring2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function h1(_a) {
2121

2222
//// [declarationEmitDestructuring2.d.ts]
2323
declare function f({x, y: [a, b, c, d]}?: {
24-
x: number;
25-
y: [number, number, number, number];
24+
x?: number;
25+
y?: [number, number, number, number];
2626
}): void;
2727
declare function g([a, b, c, d]?: [number, number, number, number]): void;
2828
declare function h([a, [b], [[c]], {x, y: [a, b, c], z: {a1, b1}}]: [any, [any], [[any]], {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/declarationEmitDestructuring4.ts(9,22): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
2+
3+
4+
==== tests/cases/compiler/declarationEmitDestructuring4.ts (1 errors) ====
5+
// For an array binding pattern with empty elements,
6+
// we will not make any modification and will emit
7+
// the similar binding pattern users' have written
8+
function baz([]) { }
9+
function baz1([] = [1,2,3]) { }
10+
function baz2([[]] = [[1,2,3]]) { }
11+
12+
function baz3({}) { }
13+
function baz4({} = { x: 10 }) { }
14+
~
15+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
16+
17+

tests/baselines/reference/declarationEmitDestructuring4.symbols

-21
This file was deleted.

tests/baselines/reference/declarationEmitDestructuring4.types

-32
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts(4,6): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
2+
tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts(4,11): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
3+
tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts(4,16): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
4+
5+
6+
==== tests/cases/compiler/declarationEmitDestructuringArrayPattern2.ts (3 errors) ====
7+
var [x10, [y10, [z10]]] = [1, ["hello", [true]]];
8+
9+
var [x11 = 0, y11 = ""] = [1, "hello"];
10+
var [a11, b11, c11] = [];
11+
~~~
12+
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
13+
~~~
14+
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
15+
~~~
16+
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
17+
18+
var [a2, [b2, { x12, y12: c2 }]=["abc", { x12: 10, y12: false }]] = [1, ["hello", { x12: 5, y12: true }]];
19+
20+
var [x13, y13] = [1, "hello"];
21+
var [a3, b3] = [[x13, y13], { x: x13, y: y13 }];
22+

tests/baselines/reference/declarationEmitDestructuringArrayPattern2.symbols

-40
This file was deleted.

tests/baselines/reference/declarationEmitDestructuringArrayPattern2.types

-70
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(2,13): error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
2+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(2,19): error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
3+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(3,23): error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.
4+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(4,16): error TS2353: Object literal may only specify known properties, and 'x5' does not exist in type '{ y5: any; }'.
5+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(6,27): error TS2353: Object literal may only specify known properties, and 'y7' does not exist in type '{ x7: any; }'.
6+
tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts(7,20): error TS2353: Object literal may only specify known properties, and 'x8' does not exist in type '{ y8: any; }'.
7+
8+
9+
==== tests/cases/compiler/declarationEmitDestructuringObjectLiteralPattern.ts (6 errors) ====
10+
11+
var { } = { x: 5, y: "hello" };
12+
~
13+
!!! error TS2353: Object literal may only specify known properties, and 'x' does not exist in type '{}'.
14+
~
15+
!!! error TS2353: Object literal may only specify known properties, and 'y' does not exist in type '{}'.
16+
var { x4 } = { x4: 5, y4: "hello" };
17+
~~
18+
!!! error TS2353: Object literal may only specify known properties, and 'y4' does not exist in type '{ x4: any; }'.
19+
var { y5 } = { x5: 5, y5: "hello" };
20+
~~
21+
!!! error TS2353: Object literal may only specify known properties, and 'x5' does not exist in type '{ y5: any; }'.
22+
var { x6, y6 } = { x6: 5, y6: "hello" };
23+
var { x7: a1 } = { x7: 5, y7: "hello" };
24+
~~
25+
!!! error TS2353: Object literal may only specify known properties, and 'y7' does not exist in type '{ x7: any; }'.
26+
var { y8: b1 } = { x8: 5, y8: "hello" };
27+
~~
28+
!!! error TS2353: Object literal may only specify known properties, and 'x8' does not exist in type '{ y8: any; }'.
29+
var { x9: a2, y9: b2 } = { x9: 5, y9: "hello" };
30+
31+
var { a: x11, b: { a: y11, b: { a: z11 }}} = { a: 1, b: { a: "hello", b: { a: true } } };
32+
33+
function f15() {
34+
var a4 = "hello";
35+
var b4 = 1;
36+
var c4 = true;
37+
return { a4, b4, c4 };
38+
}
39+
var { a4, b4, c4 } = f15();
40+
41+
module m {
42+
export var { a4, b4, c4 } = f15();
43+
}

0 commit comments

Comments
 (0)