typescript type_error ai_generated true

TS2456: Type alias 'X' circularly references itself

ID: typescript/ts-circular-reference

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

A type alias references itself directly or through a chain. Use interfaces for recursive types or break the cycle.

generic

Workarounds

  1. 90% success Use an interface instead of a type alias for recursive types
    // Instead of: type Tree = { children: Tree[] }
    // Use:
    interface Tree {
      children: Tree[];
    }

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

  2. 85% success Break the cycle with an intermediate interface
    interface JsonValue {
      [key: string]: string | number | boolean | null | JsonValue | JsonValue[];
    }

    Sources: https://www.typescriptlang.org/docs/handbook/2/objects.html

Dead Ends

Common approaches that don't work:

  1. Use any to break the circular reference 80% fail

    Loses all type safety at the recursion point, defeating the purpose of TypeScript

Error Chain

Leads to:
Preceded by:
Frequently confused with: