@@ -2896,11 +2896,12 @@ func (c *Checker) checkTypePredicate(node *ast.Node) {
2896
2896
}
2897
2897
2898
2898
func (c *Checker) getTypePredicateParent(node *ast.Node) *ast.SignatureDeclaration {
2899
- switch node.Parent.Kind {
2899
+ parent := ast.GetEffectiveTypeParent(node.Parent)
2900
+ switch parent.Kind {
2900
2901
case ast.KindArrowFunction, ast.KindCallSignature, ast.KindFunctionDeclaration, ast.KindFunctionExpression, ast.KindFunctionType,
2901
2902
ast.KindMethodDeclaration, ast.KindMethodSignature:
2902
- if node == node.Parent .Type() {
2903
- return node.Parent
2903
+ if node == parent .Type() {
2904
+ return parent
2904
2905
}
2905
2906
}
2906
2907
return nil
@@ -5377,6 +5378,11 @@ func (c *Checker) checkExportAssignment(node *ast.Node) {
5377
5378
c.error(node, diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled)
5378
5379
}
5379
5380
c.checkExternalModuleExports(container)
5381
+ if typeNode := node.Type(); typeNode != nil && node.Kind == ast.KindExportAssignment {
5382
+ t := c.getTypeFromTypeNode(typeNode)
5383
+ initializerType := c.checkExpressionCached(node.Expression())
5384
+ c.checkTypeAssignableToAndOptionallyElaborate(initializerType, t, node.Expression(), node.Expression(), nil /*headMessage*/, nil)
5385
+ }
5380
5386
if (node.Flags&ast.NodeFlagsAmbient != 0) && !ast.IsEntityNameExpression(node.Expression()) {
5381
5387
c.grammarErrorOnNode(node.Expression(), diagnostics.The_expression_of_an_export_assignment_must_be_an_identifier_or_qualified_name_in_an_ambient_context)
5382
5388
}
@@ -12579,18 +12585,11 @@ func (c *Checker) checkObjectLiteral(node *ast.Node, checkMode CheckMode) *Type
12579
12585
}
12580
12586
if ast.IsPropertyAssignment(memberDecl) || ast.IsShorthandPropertyAssignment(memberDecl) || ast.IsObjectLiteralMethod(memberDecl) {
12581
12587
var t *Type
12582
- switch {
12583
- case memberDecl.Kind == ast.KindPropertyAssignment:
12588
+ switch memberDecl.Kind {
12589
+ case ast.KindPropertyAssignment:
12584
12590
t = c.checkPropertyAssignment(memberDecl, checkMode)
12585
- case memberDecl.Kind == ast.KindShorthandPropertyAssignment:
12586
- var expr *ast.Node
12587
- if !inDestructuringPattern {
12588
- expr = memberDecl.AsShorthandPropertyAssignment().ObjectAssignmentInitializer
12589
- }
12590
- if expr == nil {
12591
- expr = memberDecl.Name()
12592
- }
12593
- t = c.checkExpressionForMutableLocation(expr, checkMode)
12591
+ case ast.KindShorthandPropertyAssignment:
12592
+ t = c.checkShorthandPropertyAssignment(memberDecl, inDestructuringPattern, checkMode)
12594
12593
default:
12595
12594
t = c.checkObjectLiteralMethod(memberDecl, checkMode)
12596
12595
}
@@ -13025,7 +13024,30 @@ func (c *Checker) checkPropertyAssignment(node *ast.Node, checkMode CheckMode) *
13025
13024
if ast.IsComputedPropertyName(node.Name()) {
13026
13025
c.checkComputedPropertyName(node.Name())
13027
13026
}
13028
- return c.checkExpressionForMutableLocation(node.Initializer(), checkMode)
13027
+ initializerType := c.checkExpressionForMutableLocation(node.Initializer(), checkMode)
13028
+ if node.Type() != nil {
13029
+ t := c.getTypeFromTypeNode(node.Type())
13030
+ c.checkTypeAssignableToAndOptionallyElaborate(initializerType, t, node, node.Initializer(), nil /*headMessage*/, nil)
13031
+ return t
13032
+ }
13033
+ return initializerType
13034
+ }
13035
+
13036
+ func (c *Checker) checkShorthandPropertyAssignment(node *ast.Node, inDestructuringPattern bool, checkMode CheckMode) *Type {
13037
+ var expr *ast.Node
13038
+ if !inDestructuringPattern {
13039
+ expr = node.AsShorthandPropertyAssignment().ObjectAssignmentInitializer
13040
+ }
13041
+ if expr == nil {
13042
+ expr = node.Name()
13043
+ }
13044
+ expressionType := c.checkExpressionForMutableLocation(expr, checkMode)
13045
+ if node.Type() != nil {
13046
+ t := c.getTypeFromTypeNode(node.Type())
13047
+ c.checkTypeAssignableToAndOptionallyElaborate(expressionType, t, node, expr, nil /*headMessage*/, nil)
13048
+ return t
13049
+ }
13050
+ return expressionType
13029
13051
}
13030
13052
13031
13053
func (c *Checker) isInPropertyInitializerOrClassStaticBlock(node *ast.Node) bool {
@@ -15683,11 +15705,15 @@ func (c *Checker) getTypeOfVariableOrParameterOrPropertyWorker(symbol *ast.Symbo
15683
15705
case ast.KindPropertyAssignment:
15684
15706
result = c.checkPropertyAssignment(declaration, CheckModeNormal)
15685
15707
case ast.KindShorthandPropertyAssignment:
15686
- result = c.checkExpressionForMutableLocation (declaration.Name() , CheckModeNormal)
15708
+ result = c.checkShorthandPropertyAssignment (declaration, true /*inDestructuringPattern*/ , CheckModeNormal)
15687
15709
case ast.KindMethodDeclaration:
15688
15710
result = c.checkObjectLiteralMethod(declaration, CheckModeNormal)
15689
15711
case ast.KindExportAssignment, ast.KindJSExportAssignment:
15690
- result = c.widenTypeForVariableLikeDeclaration(c.checkExpressionCached(declaration.AsExportAssignment().Expression), declaration, false /*reportErrors*/)
15712
+ if declaration.Type() != nil {
15713
+ result = c.getTypeFromTypeNode(declaration.Type())
15714
+ } else {
15715
+ result = c.widenTypeForVariableLikeDeclaration(c.checkExpressionCached(declaration.AsExportAssignment().Expression), declaration, false /*reportErrors*/)
15716
+ }
15691
15717
case ast.KindBinaryExpression:
15692
15718
result = c.getWidenedTypeForAssignmentDeclaration(symbol)
15693
15719
case ast.KindJsxAttribute:
@@ -17114,23 +17140,28 @@ const (
17114
17140
func (c *Checker) getWidenedTypeForAssignmentDeclaration(symbol *ast.Symbol) *Type {
17115
17141
var t *Type
17116
17142
kind, location := c.isConstructorDeclaredThisProperty(symbol)
17117
- if kind == thisAssignmentDeclarationTyped {
17143
+ switch kind {
17144
+ case thisAssignmentDeclarationTyped:
17118
17145
if location == nil {
17119
17146
panic("location should not be nil when this assignment has a type.")
17120
17147
}
17121
17148
t = c.getTypeFromTypeNode(location)
17122
- } else if kind == thisAssignmentDeclarationConstructor {
17149
+ case thisAssignmentDeclarationConstructor:
17123
17150
if location == nil {
17124
17151
panic("constructor should not be nil when this assignment is in a constructor.")
17125
17152
}
17126
17153
t = c.getFlowTypeInConstructor(symbol, location)
17127
- } else if kind == thisAssignmentDeclarationMethod {
17154
+ case thisAssignmentDeclarationMethod:
17128
17155
t = c.getTypeOfPropertyInBaseClass(symbol)
17129
17156
}
17130
17157
if t == nil {
17131
17158
var types []*Type
17132
17159
for _, declaration := range symbol.Declarations {
17133
17160
if ast.IsBinaryExpression(declaration) {
17161
+ if declaration.Type() != nil {
17162
+ t = c.getTypeFromTypeNode(declaration.Type())
17163
+ break
17164
+ }
17134
17165
types = core.AppendIfUnique(types, c.checkExpressionForMutableLocation(declaration.AsBinaryExpression().Right, CheckModeNormal))
17135
17166
}
17136
17167
}
@@ -17139,7 +17170,9 @@ func (c *Checker) getWidenedTypeForAssignmentDeclaration(symbol *ast.Symbol) *Ty
17139
17170
types = core.AppendIfUnique(types, c.undefinedOrMissingType)
17140
17171
}
17141
17172
}
17142
- t = c.getWidenedType(c.getUnionType(types))
17173
+ if t == nil {
17174
+ t = c.getWidenedType(c.getUnionType(types))
17175
+ }
17143
17176
}
17144
17177
// report an all-nullable or empty union as an implicit any in JS files
17145
17178
if symbol.ValueDeclaration != nil && ast.IsInJSFile(symbol.ValueDeclaration) &&
@@ -17160,7 +17193,7 @@ func (c *Checker) isConstructorDeclaredThisProperty(symbol *ast.Symbol) (thisAss
17160
17193
if kind, ok := c.thisExpandoKinds[symbol]; ok {
17161
17194
location, ok2 := c.thisExpandoLocations[symbol]
17162
17195
if !ok2 {
17163
- panic("ctor should be cached whenever this expando location is cached")
17196
+ panic("location should be cached whenever this expando symbol is cached")
17164
17197
}
17165
17198
return kind, location
17166
17199
}
@@ -17174,9 +17207,8 @@ func (c *Checker) isConstructorDeclaredThisProperty(symbol *ast.Symbol) (thisAss
17174
17207
bin := declaration.AsBinaryExpression()
17175
17208
if ast.GetAssignmentDeclarationKind(bin) == ast.JSDeclarationKindThisProperty &&
17176
17209
(bin.Left.Kind != ast.KindElementAccessExpression || ast.IsStringOrNumericLiteralLike(bin.Left.AsElementAccessExpression().ArgumentExpression)) {
17177
- // TODO: if bin.Type() != nil, use bin.Type()
17178
- if bin.Right.Kind == ast.KindTypeAssertionExpression {
17179
- typeAnnotation = bin.Right.AsTypeAssertion().Type
17210
+ if bin.Type != nil {
17211
+ typeAnnotation = bin.Type
17180
17212
}
17181
17213
} else {
17182
17214
allThis = false
@@ -21796,6 +21828,7 @@ func (c *Checker) getTypeFromTypeOperatorNode(node *ast.Node) *Type {
21796
21828
}
21797
21829
21798
21830
func (c *Checker) getESSymbolLikeTypeForNode(node *ast.Node) *Type {
21831
+ node = ast.GetEffectiveTypeParent(node)
21799
21832
if isValidESSymbolDeclaration(node) {
21800
21833
symbol := c.getSymbolOfNode(node)
21801
21834
if symbol != nil {
@@ -27665,7 +27698,7 @@ func (c *Checker) getContextualType(node *ast.Node, contextFlags ContextFlags) *
27665
27698
return c.getContextualType(parent, contextFlags)
27666
27699
case ast.KindSatisfiesExpression:
27667
27700
return c.getTypeFromTypeNode(parent.AsSatisfiesExpression().Type)
27668
- case ast.KindExportAssignment:
27701
+ case ast.KindExportAssignment, ast.KindJSExportAssignment, ast.KindCommonJSExport :
27669
27702
return c.tryGetTypeFromTypeNode(parent)
27670
27703
case ast.KindJsxExpression:
27671
27704
return c.getContextualTypeForJsxExpression(parent, contextFlags)
@@ -28075,11 +28108,15 @@ func (c *Checker) getContextualTypeForDecorator(decorator *ast.Node) *Type {
28075
28108
28076
28109
func (c *Checker) getContextualTypeForBinaryOperand(node *ast.Node, contextFlags ContextFlags) *Type {
28077
28110
binary := node.Parent.AsBinaryExpression()
28111
+ if t := binary.Type; t != nil {
28112
+ return c.getTypeFromTypeNode(t)
28113
+ }
28078
28114
switch binary.OperatorToken.Kind {
28079
28115
case ast.KindEqualsToken, ast.KindAmpersandAmpersandEqualsToken, ast.KindBarBarEqualsToken, ast.KindQuestionQuestionEqualsToken:
28080
28116
// In an assignment expression, the right operand is contextually typed by the type of the left operand
28081
28117
// unless it's an assignment declaration.
28082
- if node == binary.Right {
28118
+ kind := ast.GetAssignmentDeclarationKind(binary)
28119
+ if node == binary.Right && kind != ast.JSDeclarationKindModuleExports && kind != ast.JSDeclarationKindExportsProperty {
28083
28120
return c.getContextualTypeForAssignmentExpression(binary)
28084
28121
}
28085
28122
case ast.KindBarBarToken, ast.KindQuestionQuestionToken:
@@ -28171,6 +28208,9 @@ func (c *Checker) getContextualTypeForAssignmentExpression(binary *ast.BinaryExp
28171
28208
}
28172
28209
28173
28210
func (c *Checker) getContextualTypeForObjectLiteralElement(element *ast.Node, contextFlags ContextFlags) *Type {
28211
+ if t := element.Type(); t != nil && !ast.IsObjectLiteralMethod(element) {
28212
+ return c.getTypeFromTypeNode(t)
28213
+ }
28174
28214
objectLiteral := element.Parent
28175
28215
t := c.getApparentTypeOfContextualType(objectLiteral, contextFlags)
28176
28216
if t != nil {
0 commit comments