Skip to content

Commit cee708d

Browse files
committed
Remove undefined when checking patterns in function declarations
1 parent 739c083 commit cee708d

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3098,7 +3098,8 @@ namespace ts {
30983098
/** Return the inferred type for a binding element */
30993099
function getTypeForBindingElement(declaration: BindingElement): Type {
31003100
const pattern = <BindingPattern>declaration.parent;
3101-
const parentType = getTypeForBindingElementParent(<VariableLikeDeclaration>pattern.parent);
3101+
let parentType = getTypeForBindingElementParent(<VariableLikeDeclaration>pattern.parent);
3102+
31023103
// If parent has the unknown (error) type, then so does this binding element
31033104
if (parentType === unknownType) {
31043105
return unknownType;
@@ -3112,6 +3113,12 @@ namespace ts {
31123113
}
31133114
return parentType;
31143115
}
3116+
// In strict null checking mode, a default value of a binding pattern adds undefined,
3117+
// which should be removed to get the type of the elements
3118+
const func = getContainingFunction(declaration);
3119+
if (strictNullChecks && func && !func.body && getFalsyFlags(parentType) & TypeFlags.Undefined) {
3120+
parentType = getTypeWithFacts(parentType, TypeFacts.NEUndefined);
3121+
}
31153122

31163123
let type: Type;
31173124
if (pattern.kind === SyntaxKind.ObjectBindingPattern) {

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ namespace ts {
319319
}
320320
}
321321

322-
function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, type: TypeNode, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic) {
322+
function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, type: TypeNode, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic) {
323323
writer.getSymbolAccessibilityDiagnostic = getSymbolAccessibilityDiagnostic;
324324
write(": ");
325325
if (type) {

0 commit comments

Comments
 (0)