From c590c71e1d2f8b7dfa2813f5359160ac201c411c Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 10 Nov 2026 23:26:45 +0800 Subject: [PATCH] Bound template literal expansion while computing base constraints Contextually typing an object literal against a recursive conditional type built from a large catalog (next-intl's NamespaceKeys and NamespacedMessageKeys) could create tens of millions of string literal types: the base constraint computation resolves the conditional for every member of the contextual union, and each resolution distributes template literal cross products whose results are retained in the stringLiteralTypes and templateLiteralTypes caches for the lifetime of the checker. The per-call cross product check in checkCrossProductUnion bounds each distribution, but the products compound across the many distinct conditional instantiations, so memory grew quadratically with the catalog and peaked past 7 GB at 2,000 messages where TypeScript 6.0.3 stays at 2.5 GB. Count every template literal expansion performed while computing a single base constraint against a shared budget of one million expansions, and fall back to the string type once exhausted. The string type is a sound base constraint for any template literal type, and the fallback only engages in the pathological regime that previously exhausted memory. On the issue's reproduction this reduces peak RSS from 2,650 MB to 216 MB at 1,000 messages, and from an OOM kill past 7 GB to 240 MB at 2,000 messages, while producing the same four diagnostics. Fixes #65095 --- tsc/internal/checker/checker.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 950b9a8e30fb8..574d2a1982aaa 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -608,6 +608,8 @@ type Checker struct { memberLayoutCount uint32 instantiationStack []*Type conditionalConstraintDepth uint32 + templateExpansionBudget int + templateExpansionBudgetActive bool inlineLevel int serializationLevel int currentNode *ast.Node @@ -28232,6 +28234,17 @@ func (c *Checker) getResolvedBaseConstraint(t *Type, stack []RecursionId) *Type if constrained.resolvedBaseConstraint != nil { return constrained.resolvedBaseConstraint } + if !c.templateExpansionBudgetActive { + // Computing one base constraint can distribute template literal cross products over + // the members of a recursive conditional type; each distribution creates literal types + // that are retained for the checker's lifetime. Bound the total expansion performed by + // a single query (see getTemplateLiteralType). + c.templateExpansionBudgetActive = true + c.templateExpansionBudget = maxTemplateExpansionPerBaseConstraint + defer func() { + c.templateExpansionBudgetActive = false + }() + } if !c.pushTypeResolution(t, TypeSystemPropertyNameResolvedBaseConstraint) { return c.circularConstraintType } @@ -29947,9 +29960,24 @@ func (c *Checker) getConstraintDeclaration(t *Type) *ast.Node { const ( maxTemplateLiteralTypeLength = 50_000_000 maxTemplateLiteralTypeSpans = 100_000 + // Bound the number of template literal expansions performed while computing a single base + // constraint; beyond this, getTemplateLiteralType falls back to `string` (see its + // templateExpansionBudget handling). Keeps recursive conditional types from materializing + // unbounded numbers of retained literal types during contextual typing. + maxTemplateExpansionPerBaseConstraint = 1_000_000 ) func (c *Checker) getTemplateLiteralType(texts []string, types []*Type) *Type { + if c.templateExpansionBudgetActive { + // While a base constraint is being computed, every template literal expansion counts + // against a shared budget. Once exhausted, fall back to `string`, which is a sound base + // constraint for any template literal type, instead of materializing unbounded numbers + // of literal types. + if c.templateExpansionBudget <= 0 { + return c.stringType + } + c.templateExpansionBudget-- + } unionIndex := core.FindIndex(types, func(t *Type) bool { return t.flags&(TypeFlagsNever|TypeFlagsUnion) != 0 })