Add missing contextual type to static PropertyDeclarations

Makes it possible to type class static fields.

Fixes #33897.
This commit is contained in:
Eli Barzilay
2020-08-17 20:24:41 -04:00
parent 598e9b2e88
commit f9e360d44b
8 changed files with 390 additions and 38 deletions

View File

@@ -22983,7 +22983,11 @@ namespace ts {
return getContextuallyTypedParameterType(declaration);
case SyntaxKind.BindingElement:
return getContextualTypeForBindingElement(declaration);
// By default, do nothing and return undefined - only parameters and binding elements have context implied by a parent
case SyntaxKind.PropertyDeclaration:
if (hasSyntacticModifier(declaration, ModifierFlags.Static)) {
return getContextualTypeForStaticPropertyDeclaration(declaration);
}
// By default, do nothing and return undefined - only the above cases have context implied by a parent
}
}
@@ -23001,6 +23005,12 @@ namespace ts {
}
}
function getContextualTypeForStaticPropertyDeclaration(declaration: PropertyDeclaration): Type | undefined {
const parentType = isExpression(declaration.parent) && getContextualType(declaration.parent);
if (!parentType) return undefined;
return getTypeOfPropertyOfContextualType(parentType, getSymbolOfNode(declaration).escapedName);
}
// 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,