diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 208af45196b..3b0329881fd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3098,7 +3098,8 @@ namespace ts { /** Return the inferred type for a binding element */ function getTypeForBindingElement(declaration: BindingElement): Type { const pattern = declaration.parent; - const parentType = getTypeForBindingElementParent(pattern.parent); + let parentType = getTypeForBindingElementParent(pattern.parent); + // If parent has the unknown (error) type, then so does this binding element if (parentType === unknownType) { return unknownType; @@ -3112,6 +3113,12 @@ namespace ts { } return parentType; } + // In strict null checking mode, a default value of a binding pattern adds undefined, + // which should be removed to get the type of the elements + const func = getContainingFunction(declaration); + if (strictNullChecks && func && !func.body && getFalsyFlags(parentType) & TypeFlags.Undefined) { + parentType = getTypeWithFacts(parentType, TypeFacts.NEUndefined); + } let type: Type; if (pattern.kind === SyntaxKind.ObjectBindingPattern) { diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 9c8cd96280f..af32a5b4082 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -319,7 +319,7 @@ namespace ts { } } - function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, type: TypeNode, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic) { + function writeTypeOfDeclaration(declaration: AccessorDeclaration | VariableLikeDeclaration, type: TypeNode, getSymbolAccessibilityDiagnostic: GetSymbolAccessibilityDiagnostic) { writer.getSymbolAccessibilityDiagnostic = getSymbolAccessibilityDiagnostic; write(": "); if (type) {