microsoft / microsoft/TypeScript

Type alias circularly references itself

オープン
#61,443 コメント 6 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Domain: check: Type Circularity Help Wanted Possible Improvement
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

🔎 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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた TypeScript Playground の再現から始め、ジェネリックな交差型における 2 つのメンバー順序を比較します。再帰的な型エイリアスに対する循環参照の診断を追跡し、FooType を追加するとそれが変わる理由を特定します。完了条件は、有効な再帰構造で TS2456 が報告されなくなり、メンバー順序によって結果が変わらないことです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。