Report #57942
[bug\_fix] TS2589: Type instantiation is excessively deep and possibly infinite.
This error occurs when TypeScript's type checker exceeds its internal recursion depth limit \(typically 50 instantiations\) while evaluating complex recursive conditional types or deeply nested mapped types. Refactor the type definition to leverage tail-recursive evaluation patterns \(optimized in TypeScript 4.5\+\), where the recursive call appears in the 'extends' clause of a conditional type rather than in the resulting type expression. Alternatively, introduce an explicit depth counter type parameter that decrements with each recursion, providing a base case at depth 0. For inference-related depth issues, use the NoInfer utility type \(available in TypeScript 5.4\+\) to prevent TypeScript from attempting deep inference into certain type positions.
Journey Context:
A developer is constructing a sophisticated type utility library that transforms deeply nested object schemas into flattened path strings \(e.g., converting \{ a: \{ b: \{ c: string \} \} \} to 'a.b.c'\). They define a recursive conditional type 'DeepPaths' that maps over keys using 'keyof T' and recursively calls itself with the accumulated path. The type works for shallow objects, but when applied to a deeply nested database schema with 6\+ levels of nesting, the TypeScript language server hangs indefinitely in VS Code before eventually displaying 'TS2589: Type instantiation is excessively deep and possibly infinite'. The developer attempts to increase Node.js memory limits for the TypeScript server with '--max-old-space-size' but the error persists because it is a hardcoded depth limit \(50\) in the TypeScript compiler, not a memory issue. They experiment with breaking the recursion into separate type aliases, but the depth is still exceeded. Eventually, they discover that TypeScript 4.5\+ optimizes tail-recursive conditional types. They refactor their type to ensure the recursive call is in the 'extends' clause \(e.g., 'T extends infer Rest ? DeepPaths : never'\) rather than in the true/false branches of a nested conditional. This tail-recursive form allows TypeScript to eliminate the stack growth, resolving the error while preserving the type logic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:44:53.115339+00:00— report_created — created