microsoft / microsoft/TypeScript
`typeof` expressions should return an equivalent of a guard
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Search Terms
typeof guard array filter
Suggestion
Currently, typeof expressions return a boolean as expected. However, they are also special in that they narrow the types of their operands. In a similar vein, we can use type guards as the return value of functions to narrow(or assert) the types of their parameters:
declare const broad: string | number | object;
if(typeof broad === 'number') {
// broad is now a number, not a string nor an object
}
declare function isNumber(param: any): param is number;
if(isNumber(broad) {
// broad is now a number, not a string nor an object
}
This is all fine, but suppose you write a very specific function:
// Current return type: boolean
// Wish it was `param is number`
const isNumber = (param: any) => typeof param === 'number'
if (isNumber(broad)) {
// param is possibly a string or object?!
}
This is IMO a special case that should be handled; particularly, when a typeof expression(or any expression that narrows a parameter's type) is the return value of a function, TS should infer that the function is a guard.
Notice that, unlike the discussion in #6015, this issue is about the inferred type of a function which has a return value of a narrowing-expression. We could go further:
const isObject = (o: unknown) => typeof o === 'object';
// type for isObject == (o:unknown) => o is object
const isElement = (o: object) => o instanceof Element;
// type for isElement == (o:object) => o is Element
const unknownIsElement = (o: unknown) => isObject(o) && isElement(o);
// type for unknownIsElement == (o:unknown) => o is Element
const numberIsElement = (o:unknown) => isNumber(o) && isElement(o); // error: o is number so isElement can't be called
(by the way, I searched but couldn't find a similar one. If this is a duplicate I apologize!)
Use Cases
The most immediate use case is for array filter:
// error: (string|object)[] not assignable to string[]
const stringElements: string[] = elements.filter(e => typeof e === 'string');
// Works, but repetitive
const stringElements: string[] = elements.filter((e): e is string => typeof e === 'string');
I'm sure this type of change would make guards far more powerful as well, since we could create functions combine JS-checks(typeof, instanceof) or even TS-checks(like discriminant variable checking) without having to explicitly write the guard expression(which, IMO is as good as a type assertion at the moment).
Examples
(see above)
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.
This is possibly a breaking change, but IMO a good one as it would highlight actual programming errors for code that breaks
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、typeof、instanceof、composed-guard、Array.filter の issue 内の例を動作上のケースとして使用し、推論される関数の戻り値の型と制御フローによる絞り込みを担う TypeScript コンパイラーの領域を調査します。推論された述語によって、出力される JavaScript を変更せずに、示されているとおりに値が絞り込まれ、提案されているエラー動作もカバーされたら、作業は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100