microsoft / microsoft/TypeScript
Type guard should infer the type of parent object when applied on a property
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Suggestion
🔍 Search Terms
Type guard, parent object, infer, inference
✅ Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
⭐ Suggestion
Typescript should be able to infer the type of an object, if a type guard was checked on a property of the said object. Currently, Typescript does correctly infer the type of the property, but not its parent object.
📃 Motivating Example
The following example is very common in data oriented design.
interface Type1 { list: string[] }
interface Type2 { list: { [key: string]: string } }
declare const obj: Type1 | Type2;
if(Array.isArray(obj.list)) {
const list: string[] = obj.list; // WORKS
const map: { [key: string]: string } = obj.list; // ERROR, as expected
const objCasted: Type1 = obj; // ERROR, unexpectedly
} else {
const map: { [key: string]: string } = obj.list; // WORKS
const list: string[] = obj.list; // ERROR, as expected
const objCasted: Type2 = obj; // ERROR, unexpectedly
}
The following example works and that is good because it is an equally common case in this type of design.
interface Type3 { type: 'type3', data: boolean }
interface Type4 { type: 'type4', data: string }
declare const obj2: Type3 | Type4;
if(obj2.type === 'type3') {
const objCasted: Type3 = obj2; // WORKS
} else {
const objCasted: Type4 = obj2; // WORKS
}
So I believe the type guards should work the same way. As far as I see, this does not cause any inconsistency in the language or the type system. It is an improvement without any downsides.
💻 Use Cases
See the full example.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript playground で、まず動機となった 2 つの例を再現し、現在のプロパティの narrowing と親オブジェクトを比較してください。ソースファイルやテストは指定されていません。完了条件は、プロパティチェックによって両方の分岐で対応する親の union が narrowing され、実行時の出力が変わらないことです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100