Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dcb9e1c

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

50 files changed

+1,425-1,247Lines changed: 1425 additions & 1247 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+108-39Lines changed: 108 additions & 39 deletions
Large diffs are not rendered by default.
Collapse file

‎src/compiler/diagnosticInformationMap.generated.ts‎

Copy file name to clipboardExpand all lines: src/compiler/diagnosticInformationMap.generated.ts
+1Lines changed: 1 addition & 0 deletions
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." },
Collapse file

‎src/compiler/diagnosticMessages.json‎

Copy file name to clipboardExpand all lines: src/compiler/diagnosticMessages.json
+4Lines changed: 4 additions & 0 deletions
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
Collapse file

‎src/compiler/types.ts‎

Copy file name to clipboardExpand all lines: src/compiler/types.ts
+8-5Lines changed: 8 additions & 5 deletions
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 {
Collapse file

‎tests/baselines/reference/arrowFunctionExpressions.types‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/arrowFunctionExpressions.types
+3-3Lines changed: 3 additions & 3 deletions
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

Collapse file

‎tests/baselines/reference/declarationEmitDestructuring2.js‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/declarationEmitDestructuring2.js
+2-2Lines changed: 2 additions & 2 deletions
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]], {
Collapse file
+17Lines changed: 17 additions & 0 deletions
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+
Collapse file

‎tests/baselines/reference/declarationEmitDestructuring4.symbols‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/declarationEmitDestructuring4.symbols
-21Lines changed: 0 additions & 21 deletions
This file was deleted.
Collapse file

‎tests/baselines/reference/declarationEmitDestructuring4.types‎

Copy file name to clipboardExpand all lines: tests/baselines/reference/declarationEmitDestructuring4.types
-32Lines changed: 0 additions & 32 deletions
This file was deleted.
Collapse file
+22Lines changed: 22 additions & 0 deletions
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+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.