microsoft / microsoft/TypeScript

Discriminating property with never as possible type makes its enclosing object type disappear when narrowing

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

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

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

説明

🔎 Search Terms

discriminated union never, discriminating property never

🕗 Version & Regression Information

It happens in every version I tried.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241206#code/C4TwDgpgBAIglgZwMYFUB2cD2aoF4BQAPlAN5QAmiSACgE6ZgBcUaEAbhLQDRQCGzARigBfIqQpU6DZgCIw9MDJ4AjZgCYRYspWRSmUAK5pyEAGZxW5HkmYBmTfhNIANr1rQk2BMCiZm8ZHQsNHx8OFMACkwAOh0aBQBKUnwoXxSoAHoMqAA9AH58YSgIZwRoEnTMdKzcgtEwyIBCGLi9JIrUqtSa-MLi0vLK6uze4SA

💻 Code
type DiscUnion =
| { discProp: never, a: 1 }
| { discProp: "prop", b: 2 }
| { discProp: undefined, c: 3 }

declare const o: DiscUnion

if(o.discProp) {
  o
  // ^? { discProp: "prop"; b: 2; }
} else {
  o
  // ^? { discProp: undefined; c: 3; }
}

if(!o.discProp) {
  o
  // ^? { discProp: undefined; c: 3; }
} else {
  o
  // ^? { discProp: "prop"; b: 2; }
}
🙁 Actual behavior

{ discProp: never, a: 1 } disappears!

🙂 Expected behavior

In the first if, I expect { discProp: never, a: 1 } to be a possible type of o in the else branch. Viceversa, in the second if I expect it to be a possible type of o in the then branch.

Additional information about the issue

No response

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

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

はじめの一歩

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

調査の方向性

提供された TypeScript Playground の再現コードから始め、DiscUnion の例を記述どおりに実行します。if(o.discProp) と if(!o.discProp) の両方について絞り込みを追跡します。{ discProp: never, a: 1 } メンバーが期待される分岐で引き続き可能であり、他の絞り込まれたメンバーが変更されなければ作業は完了です。

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

評価

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

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

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