Report #70055
[bug\_fix] Type instantiation is excessively deep and possibly infinite. TS2589
Refactor recursive type aliases to use tail-recursion elimination with a conditional type pattern, or add a depth limiter using a counter tuple type. For DeepPartial on recursive schemas, use \`type DeepPartial = T extends object ? \{ \[K in keyof T\]?: DeepPartial \} : T;\` which TypeScript 4.5\+ can optimize. Root cause: TypeScript has a recursion depth limit \(~50\) for type instantiation; deeply nested generic types or circular type references exhaust this limit.
Journey Context:
You're building a testing utility for a complex domain model with circular references \(Category → Product → Category\). You define \`type DeepPartial = \{ \[P in keyof T\]?: DeepPartial; \};\`. It works for shallow objects. You try to use it on a deeply nested entity: \`const mock: DeepPartial = ...\`. TypeScript crashes with 'Type instantiation is excessively deep and possibly infinite'. You try to increase Node memory with --max-old-space-size, no effect. You find a GitHub issue explaining TS2589 and the ~50 level recursion limit. You discover TypeScript 4.5 introduced tail-recursion elimination for conditional types. You refactor DeepPartial to use a conditional pattern: \`type DeepPartial = T extends object ? \{ \[K in keyof T\]?: DeepPartial \} : T;\`. This allows TS to collapse the recursion. Alternatively, you realize you should just use \`Partial\` for the test and manually mock the specific nested fields needed, avoiding the recursive hell entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T00:10:07.488570+00:00— report_created — created