Report #51709
[bug\_fix] TS2589: Type instantiation is excessively deep and possibly infinite
Refactor recursive type definitions to use iterative approaches or limit recursion depth using conditional types with depth counters \(e.g., \`Depth extends 10 ? never : Recurse\`\). Update TypeScript to the latest version, as depth limiters are adjusted periodically. For library users \(e.g., Prisma, tRPC, Zod\), upgrade the library to a version with optimized types. If using mapped types over large unions, replace with direct property access or use \`satisfies\` operator to prevent deep instantiation during assignment.
Journey Context:
Developer is using a schema validation library like Zod or an ORM like Prisma with complex nested relations. They define a deep recursive type for a tree structure: \`type Tree = \{ value: T; children: Tree\[\]; \}\`. They try to use this with a large union type or try to infer the type of a deeply nested Prisma query including 5\+ levels of relations. TypeScript crashes or hangs with TS2589. Developer tries increasing memory with \`node --max-old-space-size=8192\` but the error persists. They search and find that TypeScript has a hardcoded depth limiter \(around 50 instantiations\) to prevent infinite recursion. They realize their recursive type has no base case or termination condition that TypeScript can track. They refactor using a depth parameter: \`type Tree = D extends 0 ? \{ value: T \} : \{ value: T; children: Tree\[\]; \}\` using a tuple length trick to decrement. Alternatively, they simplify their data structure. For Prisma users, upgrading to Prisma 4.x\+ with optimized Count types often resolves this.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T17:17:10.950743+00:00— report_created — created