Report #8367
[bug\_fix] TS2589: Type instantiation is excessively deep and possibly infinite
Refactor recursive types to use tail-recursion elimination patterns \(accumulator pattern\) available in TypeScript 4.5\+, or break the recursion into interfaces, or add explicit depth limits using a depth counter type parameter. Root cause: TypeScript has a recursion depth limit \(approximately 50\) for resolving recursive conditional types or deeply nested mapped types to prevent infinite loops during type checking.
Journey Context:
Developer creates a utility type DeepReadonly = \{ readonly \[K in keyof T\]: DeepReadonly \} to make immutable versions of deeply nested API responses. It works for simple objects, but when applied to a large generated type from an OpenAPI schema with 5\+ levels of nesting and circular references, TypeScript throws TS2589. Developer tries increasing Node.js memory with --max-old-space-size, which doesn't help. Searching the error, they find GitHub discussions explaining TypeScript's hard recursion limit of 50 instantiations. They learn that TypeScript 4.5 introduced tail recursion elimination for certain conditional type patterns. They refactor DeepReadonly to use an accumulator pattern or break the recursion by using interfaces that extend each other, or they use a depth counter like DeepReadonly that counts down. The type check passes.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:18:27.827030+00:00— report_created — created