microsoft / microsoft/TypeScript
Allow narrowing of unions discriminated by numeric literals using `>` `<` etc
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
🔎 Search Terms
"arguments length"
🕗 Version & Regression Information
It works the same in v3.3.3
⏯ Playground Link
💻 Code
Some basic ways to check if we have any arguments could be these:
function example1(...args: [number] | []) {
if (args.length > 0) {
return args[0];
} else {
return NaN;
}
}
function example2(...args: [number] | []) {
if (args.length) {
return args[0];
} else {
return NaN;
}
}
function example3(...args: [number] | []) {
if (args.length === 0) {
return NaN;
} else {
return args[0];
}
}
But TS doesn't seem to understand the first one.
🙁 Actual behavior
It doesn't understand the way it's done in "example1".
🙂 Expected behavior
It should understand the way it's done in "example1".
Additional information about the issue
Type inference on "args" after the check for "example1":
Type inference on "args" after the check for "example2":
Type inference on "args" after the check for "example3":
Notice how for "example2" and "example3" the type is correctly narrowed to just [number], but for "example1" the unnarowed [number] | [] type remains.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされている TypeScript Playground の例を入り口として使用し、args.length > 0 に対する制御フローの絞り込みを、既存の truthiness と equality のケースと比較します。完了条件は、example1 が true ブランチで [number] | [] を [number] に絞り込み、examples 2 と 3 に示されている動作を失わないことです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100