Remove undefined when checking patterns in function declarations

This commit is contained in:
Nathan Shively-Sanders 2016-12-21 10:50:38 -08:00
parent 739c0838e0
commit cee708df89
2 changed files with 9 additions and 2 deletions

View File

@ -3098,7 +3098,8 @@ namespace ts {
/** Return the inferred type for a binding element */
function getTypeForBindingElement(declaration: BindingElement): Type {
const pattern = <BindingPattern>declaration.parent;
const parentType = getTypeForBindingElementParent(<VariableLikeDeclaration>pattern.parent);
let parentType = getTypeForBindingElementParent(<VariableLikeDeclaration>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) {

View File

@ -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) {