Report #79647
[bug\_fix] Type instantiation is excessively deep and possibly infinite.
Refactor recursive types to use tail-recursion elimination with an accumulator type parameter, or add a depth limit counter using a tuple length check. Root cause: TypeScript has a recursion depth limit \(approx 50\); deeply nested object inference or recursive type mapping hits this limit, but TS 4.5\+ optimizes tail-recursive patterns.
Journey Context:
You're building a type-safe deep partial or deep readonly type. You write type DeepPartial = \{ \[K in keyof T\]?: DeepPartial \};. It works for shallow objects, but on a complex nested interface with circular references, TypeScript complains 'Type instantiation is excessively deep and possibly infinite'. You try adding constraints but the error persists. You search GitHub and find issue \#30118 and PR \#45711. You learn about tail recursion elimination introduced in TypeScript 4.5. The technique involves adding an accumulator type parameter that builds the result, allowing TypeScript to optimize the recursion. For example, type DeepPartial = T extends object ? ... : Acc;. Alternatively, you add a 'depth limit' counter using a tuple type that decrements: type DeepPartial = \[D\] extends \[never\] ? T : ...;. By limiting depth or using tail-recursion, the type instantiation stays within limits. You refactor your type to use one of these patterns, and the error disappears while preserving the utility of the type.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T16:17:29.565994+00:00— report_created — created