typescript type_error ai_generated partial

error TS2589: Type instantiation is excessively deep and possibly infinite

ID: typescript/ts2589-excessive-stack-depth

Also available as: JSON · Markdown
78%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

Recursive type exceeded depth limit. Complex conditional/mapped types or circular type references.

generic

Workarounds

  1. 88% success Simplify the recursive type — add a depth limit parameter
    type DeepPartial<T, D extends number = 5> = ...

    Sources: https://www.typescriptlang.org/docs/handbook/2/conditional-types.html

  2. 82% success Break circular type references by using interface instead of type alias
    // Break circular type by using interface:
    // Bad (circular type alias):
    type Tree = { children: Tree[] }; // may cause recursion
    
    // Good (interface handles recursion natively):
    interface Tree {
      children: Tree[];
    }

    Sources: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces

  3. 78% success Check if a simpler type utility exists in a library like type-fest
    Check if a simpler type utility exists in a library like type-fest

    Sources: https://www.npmjs.com/package/type-fest

Dead Ends

Common approaches that don't work:

  1. Increase TypeScript recursion limit 90% fail

    No such config option — the limit is hardcoded for safety

  2. Use @ts-ignore on the line 65% fail

    Hides the type entirely — you lose all type checking

Error Chain

Preceded by: