Skip to content

Commit 07d7ec3

Browse files
authored
Merge pull request #9422 from Microsoft/allow-destructuring-assignment-with-default-of-undefined-optional-property
Destructuring assignment removes undefined from type when default value is given
2 parents 7b77f6b + 5f6e25c commit 07d7ec3

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12544,6 +12544,12 @@ namespace ts {
1254412544
if (exprOrAssignment.kind === SyntaxKind.ShorthandPropertyAssignment) {
1254512545
const prop = <ShorthandPropertyAssignment>exprOrAssignment;
1254612546
if (prop.objectAssignmentInitializer) {
12547+
// In strict null checking mode, if a default value of a non-undefined type is specified, remove
12548+
// undefined from the final type.
12549+
if (strictNullChecks &&
12550+
!(getCombinedTypeFlags(checkExpression(prop.objectAssignmentInitializer)) & TypeFlags.Undefined)) {
12551+
sourceType = getTypeWithFacts(sourceType, TypeFacts.NEUndefined);
12552+
}
1254712553
checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, contextualMapper);
1254812554
}
1254912555
target = (<ShorthandPropertyAssignment>exprOrAssignment).name;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [destructuringAssignmentWithDefault.ts]
2+
const a: { x?: number } = { };
3+
let x = 0;
4+
({x = 1} = a);
5+
6+
7+
//// [destructuringAssignmentWithDefault.js]
8+
var a = {};
9+
var x = 0;
10+
(_a = a.x, x = _a === void 0 ? 1 : _a, a);
11+
var _a;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/destructuringAssignmentWithDefault.ts ===
2+
const a: { x?: number } = { };
3+
>a : Symbol(a, Decl(destructuringAssignmentWithDefault.ts, 0, 5))
4+
>x : Symbol(x, Decl(destructuringAssignmentWithDefault.ts, 0, 10))
5+
6+
let x = 0;
7+
>x : Symbol(x, Decl(destructuringAssignmentWithDefault.ts, 1, 3))
8+
9+
({x = 1} = a);
10+
>x : Symbol(x, Decl(destructuringAssignmentWithDefault.ts, 2, 2))
11+
>a : Symbol(a, Decl(destructuringAssignmentWithDefault.ts, 0, 5))
12+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/destructuringAssignmentWithDefault.ts ===
2+
const a: { x?: number } = { };
3+
>a : { x?: number | undefined; }
4+
>x : number | undefined
5+
>{ } : {}
6+
7+
let x = 0;
8+
>x : number
9+
>0 : number
10+
11+
({x = 1} = a);
12+
>({x = 1} = a) : { x?: number | undefined; }
13+
>{x = 1} = a : { x?: number | undefined; }
14+
>{x = 1} : { x?: number; }
15+
>x : number
16+
>a : { x?: number | undefined; }
17+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @strictNullChecks: true
2+
const a: { x?: number } = { };
3+
let x = 0;
4+
({x = 1} = a);

0 commit comments

Comments
 (0)