Report #10813
[bug\_fix] Type instantiation is excessively deep and possibly infinite when using recursive conditional or mapped types
TypeScript has a hard limit \(default 50\) on type instantiation depth to prevent infinite recursion. When defining recursive utility types \(e.g., DeepPartial, DeepReadonly, or object path type builders\), this limit is quickly exhausted. To fix, refactor the type to use Tail-Recursion Elimination \(TRE\) patterns available in TypeScript 4.5\+, where the recursive call is in the 'true' branch of a conditional type and accumulates results in an 'output' type parameter. Alternatively, explicitly limit recursion depth using a depth counter \(e.g., \`Depth extends number = 5\` and a \`DepthCounter\` type that decrements until reaching a base case\). As a last resort, simplify the type to be less recursive or use iterative logic at runtime instead of type-level recursion.
Journey Context:
Developer attempts to create a type-safe path accessor for nested objects, defining \`type Path = T extends object ? \{ \[K in keyof T\]: K \| \`$\{K & string\}.$\{Path\}\` \}\[keyof T\] : never\`. On shallow objects \(depth 2-3\), this works and provides autocomplete. However, when applying it to a deeply nested API response type \(depth 4\+\), the TypeScript language server hangs for several seconds before reporting 'Type instantiation is excessively deep and possibly infinite'. The developer first tries to increase the recursion limit using a hypothetical compiler flag, discovering none exists. They then attempt to add 'base case' conditions like \`T extends string ? never : ...\` but the error persists because the mapped type still attempts to evaluate the full recursion tree. Researching the error leads to TypeScript 4.5's release notes on Tail Recursion Elimination. The developer refactors the type to use an accumulator pattern: \`type Path = T extends object ? \{ \[K in keyof T\]: Path \}\[keyof T\] : Acc\`. By moving the recursive result into an accumulator parameter and ensuring the recursive call is in a tail position, the type instantiation depth no longer grows with object depth, resolving the error.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:44:36.896152+00:00— report_created — created