microsoft / microsoft/TypeScript

Allow narrowing of unions discriminated by numeric literals using `>` `<` etc

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

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

Awaiting More Feedback Suggestion
主要言語
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

https://www.typescriptlang.org/play/?target=99&module=0&ts=5.8.3#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsgRgAoA6M9AJwHMBnALkQG0wRMAjZCgXUQB8muASkQBvAFCJJiGMERFKtEvjBUoAC0QA+RAAZh4qYcQVkUEBSQKajHVwDcEqQF8UuGslGOjx0+aQA5dH8HQycxMNBIWAQUDBx8ACZScmp6JhZ2Th5+RiFPQxk5KyVkFXV9LyMTMwtEKxt7SpdkNw8Dbx8agKCQ53CxSOh4JDQsPGQAZmSSKwZmVg5uPgEKgtl5VJKyjQBePd1Vjuq-REDgptd3fKPfWvrbXskwpyA

💻 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":

Image

Type inference on "args" after the check for "example2":

Image

Type inference on "args" after the check for "example3":

Image

Notice how for "example2" and "example3" the type is correctly narrowed to just [number], but for "example1" the unnarowed [number] | [] type remains.

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

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

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. 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

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

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