Merge pull request #7298 from Microsoft/contextually-type-binding-initializers

Contextually type binding initializers
This commit is contained in:
Nathan Shively-Sanders
2016-02-29 14:26:34 -08:00
8 changed files with 454 additions and 6 deletions

View File

@@ -2604,7 +2604,7 @@ namespace ts {
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteral((<ComputedPropertyName>name).expression.kind);
}
// Return the inferred type for a binding element
/** Return the inferred type for a binding element */
function getTypeForBindingElement(declaration: BindingElement): Type {
const pattern = <BindingPattern>declaration.parent;
const parentType = getTypeForBindingElementParent(<VariableLikeDeclaration>pattern.parent);
@@ -2630,6 +2630,9 @@ namespace ts {
// computed properties with non-literal names are treated as 'any'
return anyType;
}
if (declaration.initializer) {
getContextualType(declaration.initializer);
}
// Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature,
// or otherwise the type of the string index signature.
@@ -7825,11 +7828,14 @@ namespace ts {
return undefined;
}
// In a variable, parameter or property declaration with a type annotation, the contextual type of an initializer
// expression is the type of the variable, parameter or property. Otherwise, in a parameter declaration of a
// contextually typed function expression, the contextual type of an initializer expression is the contextual type
// of the parameter. Otherwise, in a variable or parameter declaration with a binding pattern name, the contextual
// type of an initializer expression is the type implied by the binding pattern.
// In a variable, parameter or property declaration with a type annotation,
// the contextual type of an initializer expression is the type of the variable, parameter or property.
// Otherwise, in a parameter declaration of a contextually typed function expression,
// the contextual type of an initializer expression is the contextual type of the parameter.
// Otherwise, in a variable or parameter declaration with a binding pattern name,
// the contextual type of an initializer expression is the type implied by the binding pattern.
// Otherwise, in a binding pattern inside a variable or parameter declaration,
// the contextual type of an initializer expression is the type annotation of the containing declaration, if present.
function getContextualTypeForInitializerExpression(node: Expression): Type {
const declaration = <VariableLikeDeclaration>node.parent;
if (node === declaration.initializer) {
@@ -7845,6 +7851,18 @@ namespace ts {
if (isBindingPattern(declaration.name)) {
return getTypeFromBindingPattern(<BindingPattern>declaration.name, /*includePatternInType*/ true);
}
if (isBindingPattern(declaration.parent)) {
const parentDeclaration = declaration.parent.parent;
const name = declaration.propertyName || declaration.name;
if (isVariableLike(parentDeclaration) &&
parentDeclaration.type &&
!isBindingPattern(name)) {
const text = getTextOfPropertyName(name);
if (text) {
return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text);
}
}
}
}
return undefined;
}