microsoft / microsoft/TypeScript

String union types narrowed to falsy should narrow string to ""

オープン
#45,329 コメント 6 件 リアクション 9 件 担当者 0 名 GitHub で見る

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

string truthy falsy narrow literal empty

🕗 Version & Regression Information
  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing
  • I was unable to test this on prior versions because _______
⏯ Playground Link

Playground link with relevant code

💻 Code
let value = Math.random() > 0.5 && "Some Name";

if (!value) {
  value; // Type: false | string, but should be false | ""
}
🙁 Actual behavior

Inside the narrowed block, value should be false | "", because no other string value is falsy.

🙂 Expected behavior

It's the more general false | string instead.

This was a hard issue to search for; #41503, #33878, and #31156 seemed related but not duplicate to me.

Shoutout to RyanCavanaugh for sending me a code snippet that looked like this -- no good deed goes unpunished in the issue tracker 😄

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

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

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンク先の TypeScript Playground で narrowing のケースを再現し、その後、issues #41503、#33878、#31156 の関連する議論を読みます。falsy な文字列のユニオンに対する型チェックの挙動を追跡し、narrowing 後の結果が false と空文字列を区別することを確認します。回帰がカバーされ、報告された型が修正されれば issue は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

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

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