Report #5798
[bug\_fix] error TS2589: Type instantiation is excessively deep and possibly infinite.
Refactor the recursive type to use tail-recursion elimination with an accumulator pattern, or add a depth limit counter that decrements on each recursive step \(e.g., \`type DeepType = Depth extends 0 ? never : ...\`\). For TypeScript 4.5\+, ensure the recursive path returns the accumulator directly without intermediate nested conditional types. The root cause is that TypeScript limits type instantiation depth \(to 50 or 100 levels depending on the pattern\) to prevent infinite recursion during type checking. Deeply nested object paths, recursive JSON types, or deep partial utilities exceed this limit.
Journey Context:
Developer creates a utility type to generate deeply nested path strings for an object: \`type Paths = T extends object ? \{ \[K in keyof T\]: \`$\{K & string\}.$\{Paths\}\` \}\[keyof T\] : never\`. When applied to an object with 6\+ levels of nesting, TypeScript throws TS2589. Developer attempts to increase the limit via compiler flags but finds no such option \(it's hardcoded\). They try to break the recursion with a conditional check \`T extends object ? ... : never\` but the error persists because the recursion depth is still too high. They search GitHub issues and find references to tail-recursion elimination added in TypeScript 4.5. They refactor the type to pass an accumulator array that builds the path, returning the accumulator when the base case is reached, rather than concatenating strings in nested conditional types. This flattens the recursion depth. Alternatively, they add a depth counter \`Depth extends number = 10\` that decrements \`Depth extends 0 ? never : ...\` to enforce a hard limit.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T22:13:12.329255+00:00— report_created — created