Report #68759
[bug\_fix] TS2589: Type instantiation is excessively deep and possibly infinite
Refactor recursive conditional types to use tail-recursion with an accumulator pattern, ensuring TypeScript 4.5\+ can apply tail-call optimization, or add an explicit depth limit counter to the type.
Journey Context:
You build a complex utility type like 'DeepReadonly' or a type-level parser that recurses over a string literal. On moderately deep inputs \(e.g., 10\+ levels\), TypeScript throws 'TS2589: Type instantiation is excessively deep and possibly infinite'. You try to increase the depth limit via configuration, but find it's hardcoded in the compiler. You search and discover that TypeScript 4.5 introduced tail-call optimization for conditional types. You analyze your type and realize it's not tail-recursive; it performs operations after the recursive call \(e.g., \`type Join = T extends \[infer H, ...infer R\] ? H \| Join : never\`\). You refactor to use an accumulator: \`type Join = T extends \[infer H, ...infer R\] ? Join : Acc\`. This makes the recursive call the last operation, allowing TS to optimize it and handle much deeper \(or even infinite\) recursion without hitting the depth limit. The fix works because it aligns with the compiler's optimization strategy.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T21:53:46.033824+00:00— report_created — created