debugtypescriptModeratepending
Debug: TypeScript 'Type instantiation is excessively deep'
Viewed 0 times
TS2589recursive-typedeepinfinitetype-instantiation
Error Messages
Problem
TypeScript compiler error TS2589: Type instantiation is excessively deep and possibly infinite.
Solution
This happens with deeply recursive types. Fixes:
// BAD: Unbounded recursion
type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> };
// GOOD: Add depth counter
type DeepPartial<T, Depth extends any[] = []> =
Depth['length'] extends 10 ? T :
{ [K in keyof T]?: DeepPartial<T[K], [...Depth, any]> };
// If you have Type1<Type2<Type3<Type4<T>>>>, flatten the nesting
const result = complexOperation() as ExpectedType;
type A = { b: B };
type B = { a: A }; // Mutual recursion can trigger this
- Add recursion depth limit:
// BAD: Unbounded recursion
type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> };
// GOOD: Add depth counter
type DeepPartial<T, Depth extends any[] = []> =
Depth['length'] extends 10 ? T :
{ [K in keyof T]?: DeepPartial<T[K], [...Depth, any]> };
- Simplify generic chains:
// If you have Type1<Type2<Type3<Type4<T>>>>, flatten the nesting
- Use type assertions as escape hatch:
const result = complexOperation() as ExpectedType;
- Check for circular type references:
type A = { b: B };
type B = { a: A }; // Mutual recursion can trigger this
- Update TypeScript — newer versions have higher limits and better detection.
Revisions (0)
No revisions yet.