Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12544,6 +12544,12 @@ namespace ts {
if (exprOrAssignment.kind === SyntaxKind.ShorthandPropertyAssignment) {
const prop = <ShorthandPropertyAssignment>exprOrAssignment;
if (prop.objectAssignmentInitializer) {
// In strict null checking mode, if a default value of a non-undefined type is specified, remove
// undefined from the final type.
if (strictNullChecks &&
!(getCombinedTypeFlags(checkExpressionCached(prop.objectAssignmentInitializer)) & TypeFlags.Undefined)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use checkExpression here, not checkExpressionCached. The latter should only be used (a) when you know that contextual types will never have an effect on the expression and (b) when other checks of the same expression also use the caching version. Neither are true here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

sourceType = getTypeWithFacts(sourceType, TypeFacts.NEUndefined);
}
checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, contextualMapper);
}
target = (<ShorthandPropertyAssignment>exprOrAssignment).name;
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/destructuringAssignmentWithDefault.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [destructuringAssignmentWithDefault.ts]
const a: { x?: number } = { };
let x = 0;
({x = 1} = a);


//// [destructuringAssignmentWithDefault.js]
var a = {};
var x = 0;
(_a = a.x, x = _a === void 0 ? 1 : _a, a);
var _a;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/compiler/destructuringAssignmentWithDefault.ts ===
const a: { x?: number } = { };
>a : Symbol(a, Decl(destructuringAssignmentWithDefault.ts, 0, 5))
>x : Symbol(x, Decl(destructuringAssignmentWithDefault.ts, 0, 10))

let x = 0;
>x : Symbol(x, Decl(destructuringAssignmentWithDefault.ts, 1, 3))

({x = 1} = a);
>x : Symbol(x, Decl(destructuringAssignmentWithDefault.ts, 2, 2))
>a : Symbol(a, Decl(destructuringAssignmentWithDefault.ts, 0, 5))

17 changes: 17 additions & 0 deletions tests/baselines/reference/destructuringAssignmentWithDefault.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/destructuringAssignmentWithDefault.ts ===
const a: { x?: number } = { };
>a : { x?: number | undefined; }
>x : number | undefined
>{ } : {}

let x = 0;
>x : number
>0 : number

({x = 1} = a);
>({x = 1} = a) : { x?: number | undefined; }
>{x = 1} = a : { x?: number | undefined; }
>{x = 1} : { x?: number; }
>x : number
>a : { x?: number | undefined; }

4 changes: 4 additions & 0 deletions tests/cases/compiler/destructuringAssignmentWithDefault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @strictNullChecks: true
const a: { x?: number } = { };
let x = 0;
({x = 1} = a);