@@ -1644,6 +1644,21 @@ func getJSDocTypeAssertionType(node *ast.Node) *ast.TypeNode {
1644
1644
return nil
1645
1645
}
1646
1646
1647
+ // getTypeForDeclaration resolves the type for a declaration, considering various special cases.
1648
+ func (b * nodeBuilderImpl ) getTypeForDeclaration (symbol * ast.Symbol , declaration * ast.Declaration ) * Type {
1649
+ t := b .ctx .enclosingSymbolTypes [ast .GetSymbolId (symbol )]
1650
+ if t == nil {
1651
+ if symbol .Flags & ast .SymbolFlagsAccessor != 0 && declaration != nil && declaration .Kind == ast .KindSetAccessor {
1652
+ t = b .ch .instantiateType (b .ch .getWriteTypeOfSymbol (symbol ), b .ctx .mapper )
1653
+ } else if symbol != nil && (symbol .Flags & (ast .SymbolFlagsTypeLiteral | ast .SymbolFlagsSignature ) == 0 ) {
1654
+ t = b .ch .instantiateType (b .ch .getWidenedLiteralType (b .ch .getTypeOfSymbol (symbol )), b .ctx .mapper )
1655
+ } else {
1656
+ t = b .ch .errorType
1657
+ }
1658
+ }
1659
+ return t
1660
+ }
1661
+
1647
1662
func (b * nodeBuilderImpl ) serializeTypeForExpression (expr * ast.Node ) * ast.Node {
1648
1663
// !!! TODO: shim, add node reuse
1649
1664
t := b .ch .instantiateType (b .ch .getWidenedType (b .ch .getRegularTypeOfExpression (expr )), b .ctx .mapper )
@@ -1998,28 +2013,22 @@ func (b *nodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
1998
2013
// Check for PropertyAssignment with type assertion that can be reused (PR #60576)
1999
2014
if declaration != nil && declaration .Kind == ast .KindPropertyAssignment {
2000
2015
initializer := declaration .AsPropertyAssignment ().Initializer
2016
+ initializerKind := initializer .Kind
2001
2017
var assertionNode * ast.TypeNode
2002
2018
2003
2019
// Check if initializer is a JSDoc type assertion
2004
2020
if ast .IsJSDocTypeAssertion (initializer ) {
2005
2021
assertionNode = getJSDocTypeAssertionType (initializer )
2006
- } else if initializer . Kind == ast .KindAsExpression || initializer . Kind == ast .KindTypeAssertionExpression {
2022
+ } else if initializerKind == ast .KindAsExpression || initializerKind == ast .KindTypeAssertionExpression {
2007
2023
// Extract the type node from AsExpression or TypeAssertion
2008
- if initializer .Kind == ast .KindAsExpression {
2009
- assertionNode = initializer .AsAsExpression ().Type
2010
- } else {
2011
- assertionNode = initializer .AsTypeAssertion ().Type
2012
- }
2024
+ assertionNode = getAssertedTypeNode (initializer )
2013
2025
}
2014
2026
2015
2027
// If we have an assertion node, check if we can reuse it
2016
2028
if assertionNode != nil && ! isConstTypeReference (assertionNode ) {
2017
2029
// Try to reuse the existing type node if it's equivalent
2018
2030
if t == nil {
2019
- t = b .ctx .enclosingSymbolTypes [ast .GetSymbolId (symbol )]
2020
- if t == nil && symbol != nil && (symbol .Flags & (ast .SymbolFlagsTypeLiteral | ast .SymbolFlagsSignature ) == 0 ) {
2021
- t = b .ch .instantiateType (b .ch .getWidenedLiteralType (b .ch .getTypeOfSymbol (symbol )), b .ctx .mapper )
2022
- }
2031
+ t = b .getTypeForDeclaration (symbol , declaration )
2023
2032
}
2024
2033
2025
2034
if t != nil {
@@ -2032,16 +2041,7 @@ func (b *nodeBuilderImpl) serializeTypeForDeclaration(declaration *ast.Declarati
2032
2041
}
2033
2042
2034
2043
if t == nil {
2035
- t = b .ctx .enclosingSymbolTypes [ast .GetSymbolId (symbol )]
2036
- if t == nil {
2037
- if symbol .Flags & ast .SymbolFlagsAccessor != 0 && declaration .Kind == ast .KindSetAccessor {
2038
- t = b .ch .instantiateType (b .ch .getWriteTypeOfSymbol (symbol ), b .ctx .mapper )
2039
- } else if symbol != nil && (symbol .Flags & (ast .SymbolFlagsTypeLiteral | ast .SymbolFlagsSignature ) == 0 ) {
2040
- t = b .ch .instantiateType (b .ch .getWidenedLiteralType (b .ch .getTypeOfSymbol (symbol )), b .ctx .mapper )
2041
- } else {
2042
- t = b .ch .errorType
2043
- }
2044
- }
2044
+ t = b .getTypeForDeclaration (symbol , declaration )
2045
2045
}
2046
2046
2047
2047
// !!! TODO: JSDoc, getEmitResolver call is unfortunate layering for the helper - hoist it into checker
0 commit comments