microsoft / microsoft/TypeScript
Lazier typeof operator to enable limited "circular" references
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Search Terms
- typeof referenced indirectly
- lazy typeof
- lazier typeof
- circular typeof
#25214 is very closely related but not exactly the same.
Suggestion
If typeof operators were resolved slightly lazier, certain "circular" type definitions could be typed more automatically. Then the following could be made to compile:
type Place = keyof typeof building;
function room(doors: Place[]): {doors: Place[]} {
return { doors };
}
const building = {
foyer: room(["dining", "kitchen"]),
dining: room(["foyer", "bedroom"]),
kitchen: room(["foyer", "bedroom"]),
bedroom: room(["dining", "kitchen"]),
};
Currently, you get
Type alias 'Place' circularly references itself.ts(2456)
However, Place doesn't actually reference itself since you don't need to know what Place is to evaluate keyof typeof building, only to evaluate typeof building in its entirety. If typeof building were resolved lazily (as it was needed) then its keys could be found without needing Place, breaking the reference cycle.
Specifically, we can resolve type Place = "foyer" | "dining" | "kitchen" | "bedroom" without needing to know what building's properties are specifically typed. Then, we can proceed with any inferences as needed for the building, getting the complete inferred type
type Place = "foyer" | "dining" | "kitchen" | "bedroom";
const building: Record<Place, {doors: Place[]}>; // or something equivalent, e.g. an actual object
Downsides/Tradeoffs
- More complexity in type-checker
- Relatively fringe use-case
- Possibly brittle (small changes to variable definition or type definition may cause circularity to reappear, limiting possible use)
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、issue の TypeScript 例を再現し、#25214 の関連する議論と比較します。keyof typeof と循環型エイリアスに対する type-checker の動作を調査します。完了の条件は、正しい推論を維持し、リグレッションを避けた状態で例がコンパイルできることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100