typescript
type_error
ai_generated
true
TS2456: Type alias 'X' circularly references itself
ID: typescript/ts-circular-reference
83%Fix Rate
86%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 5 | active | — | — | — |
Root Cause
A type alias references itself directly or through a chain. Use interfaces for recursive types or break the cycle.
genericWorkarounds
-
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
-
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:
-
Use any to break the circular reference
80% fail
Loses all type safety at the recursion point, defeating the purpose of TypeScript
Error Chain
Preceded by:
Frequently confused with: