diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index b88f66cf965e2..19faeea52511b 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -13905,12 +13905,29 @@ func (c *Checker) hasDefaultValue(node *ast.Node) bool { } func (c *Checker) isConstContext(node *ast.Node) bool { + isConst, _, _ := c.isConstContextEx(node) + return isConst +} + +func (c *Checker) isConstContextEx(node *ast.Node) (isConst bool, contextualType *Type, computedContextual bool) { parent := node.Parent - return ast.IsConstAssertion(parent) || - c.isInlineImportAttributes(node) || - c.isValidConstAssertionArgument(node) && c.isConstTypeVariable(c.getContextualType(node, ContextFlagsNone), 0) || - (ast.IsParenthesizedExpression(parent) || ast.IsArrayLiteralExpression(parent) || ast.IsSpreadElement(parent)) && c.isConstContext(parent) || - (ast.IsPropertyAssignment(parent) || ast.IsShorthandPropertyAssignment(parent) || ast.IsTemplateSpan(parent)) && c.isConstContext(parent.Parent) + if ast.IsConstAssertion(parent) || c.isInlineImportAttributes(node) { + return true, nil, false + } + if c.isValidConstAssertionArgument(node) { + contextualType = c.getContextualType(node, ContextFlagsNone) + computedContextual = true + if c.isConstTypeVariable(contextualType, 0) { + return true, contextualType, true + } + } + if (ast.IsParenthesizedExpression(parent) || ast.IsArrayLiteralExpression(parent) || ast.IsSpreadElement(parent)) && c.isConstContext(parent) { + return true, contextualType, computedContextual + } + if (ast.IsPropertyAssignment(parent) || ast.IsShorthandPropertyAssignment(parent) || ast.IsTemplateSpan(parent)) && c.isConstContext(parent.Parent) { + return true, contextualType, computedContextual + } + return false, contextualType, computedContextual } func (c *Checker) isInlineImportAttributes(node *ast.Node) bool { @@ -13955,11 +13972,22 @@ func (c *Checker) isConstTypeVariable(t *Type, depth int) bool { if depth >= 5 || t == nil { return false } + if t.objectFlags&(ObjectFlagsCouldContainTypeVariablesComputed|ObjectFlagsCouldContainTypeVariables) == ObjectFlagsCouldContainTypeVariablesComputed { + return false + } switch { case t.flags&TypeFlagsTypeParameter != 0: return t.symbol != nil && core.Some(t.symbol.Declarations(), func(d *ast.Node) bool { return ast.HasSyntacticModifier(d, ast.ModifierFlagsConst) }) case t.flags&TypeFlagsUnionOrIntersection != 0: - return core.Some(t.Types(), func(s *Type) bool { return c.isConstTypeVariable(s, depth) }) + if t.objectFlags&ObjectFlagsPrimitiveUnion != 0 { + return false + } + for _, s := range t.Types() { + if c.isConstTypeVariable(s, depth) { + return true + } + } + return false case t.flags&TypeFlagsIndexedAccess != 0: return c.isConstTypeVariable(t.AsIndexedAccessType().objectType, depth+1) case t.flags&TypeFlagsConditional != 0: @@ -14186,13 +14214,17 @@ func (c *Checker) checkObjectLiteralMethod(node *ast.Node, checkMode CheckMode) func (c *Checker) checkExpressionForMutableLocation(node *ast.Node, checkMode CheckMode) *Type { t := c.checkExpressionEx(node, checkMode) + isConst, contextualType, computedContextual := c.isConstContextEx(node) switch { - case c.isConstContext(node): + case isConst: return c.getRegularTypeOfLiteralType(t) case isTypeAssertion(node): return t default: - return c.getWidenedLiteralLikeTypeForContextualType(t, c.instantiateContextualType(c.getContextualType(node, ContextFlagsNone), node, ContextFlagsNone)) + if !computedContextual { + contextualType = c.getContextualType(node, ContextFlagsNone) + } + return c.getWidenedLiteralLikeTypeForContextualType(t, c.instantiateContextualType(contextualType, node, ContextFlagsNone)) } }