mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-24 20:44:53 -05:00
Add missing contextual type to static PropertyDeclarations
Makes it possible to type class static fields. Fixes #33897.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user