Microsoft/TypeScript

Type alias circularly references itself

Open

#61,443 opened on 2025幎3月18日

GitHub で芋る
 (6 comments) (2 reactions) (0 assignees)TypeScript (6,726 forks)batch import
Domain: check: Type CircularityHelp WantedPossible Improvement

Repository metrics

Stars
 (48,455 stars)
PR merge metrics
 (平均マヌゞ 6d 17h) (30d で 9 merged PRs)

説明

🔎 Search Terms

circular reference, generic, error TS2456, recursive

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about circular reference

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.2#code/C4TwDgpgBAYg9nAKuaBeKBvAsAKClAMwQH4AuKAIwQBsIBDAOwG5cBfFnXUSKAIToDOEAKK0AthAbAAPIgB8UdPCQooAMky58AYwAWAS2oATAE6TyAYQCuA4HDGiIEqQG0Auh3zcI5RB3a4XKrWtvaOzsCKfIIi4pIyAOS6AIwJchxAA

💻 Code

type FooType = {
  foo?: boolean;
};

type BaseElement<T> = FooType & {
  children: CustomElement[];
  type: T;
};

type CustomElement = BaseElement<'h1'>;

🙁 Actual behavior

When defining a recursive type structure using a generic type, TypeScript incorrectly reports a Type alias 'CustomElement' circularly references itself.(2456) error, even though the type should be resolvable. It gets weird when you move type: T; line above the children: CustomElement[]; and the error disappears. Also removing FooType will resolve the issue.

🙂 Expected behavior

In the first place the code should not throw any errors, in the second place the behavior should not change by reordering records of a type.

Additional information about the issue

No response

コントリビュヌタヌガむド