Skip to content
Latchkey

TypeScript "error TS2589: Type instantiation is excessively deep" in CI

The compiler hit its instantiation-depth guard while resolving a recursive or very complex generic type. TS2589 protects against runaway/infinite type computation, but it fails the build.

What this error means

A build fails with TS2589 on a heavily generic expression. It may appear only after a dependency type upgrade or when types grow more complex.

node
src/query.ts:42:18 - error TS2589: Type instantiation is excessively
deep and possibly infinite.

42   const result = deepMerge(a, b);
                    ~~~~~~~~~~~~~~~

Common causes

A recursive generic without a base case

A conditional/mapped type recurses too deeply (or unboundedly), so the compiler stops at its depth limit.

An overly complex inferred type

Chained generic helpers infer a type graph deep enough to trip the guard even though it would eventually terminate.

How to fix it

Add a recursion limit or break the chain

Cap recursion depth in the generic, or split the expression so the compiler infers smaller pieces.

src/types.ts
type DeepMerge<A, B, Depth extends number = 5> =
  Depth extends 0 ? A : /* ...bounded recursion... */ A;

Annotate to stop inference

Provide an explicit type annotation so the compiler does not have to instantiate the deep generic.

src/query.ts
const result: MergedShape = deepMerge(a, b);

How to prevent it

  • Bound recursion in custom generic types with a depth parameter.
  • Annotate complex call results to avoid deep inference.
  • Pin dependency types and review type-heavy upgrades.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →