microsoft / microsoft/TypeScript

Type predicate for an optional property is not inferred

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

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

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

説明

🔎 Search Terms

inferred type predicate optional

🕗 Version & Regression Information
  • This changed between versions 5.4 and 5.5
⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.4#code/C4TwDgpgBA8gRgKygXigbwPZmASwwOwEMAbAfgC51DL8BXAWzggCcBfVgKFElkQCYU6LLgIkKUWvgAmEAGY58EKaygAfIdjxFilNNSh1GLdh1OzJAYxH4oOAM4xNo4gBE5CpQAoIleAgCU6BwA9MFQ4RFQAHqkHJHMEMC0zDYQAHTCWiRQAITIqJIy8opSHJwc5vhWWrYOTtpuxUp83r78gWghYTFxEQlJKVDpmc65+RLS7iVlHEA

💻 Code
type Obj = {optional?: {a: number}}

function isOptionalDefined(e: Obj) {
    return e.optional !== undefined
}

if (isOptionalDefined(someObj) {
  someObj.optional.a // 'someObj.optional' is possibly 'undefined'
}
🙁 Actual behavior

If a type contains an optional property, and I write a function that ensures the property is defined, the function is not inferred as a type predicate. No type narrowing happens when I use this function.

At the same time, if I rewrite the original type to be itself a union, then the type predicate is inferred, as expected.

🙂 Expected behavior

Type predicate is inferred

Additional information about the issue

I feel it has something to do with https://github.com/microsoft/TypeScript/issues/55257, but I'm not sure.

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

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

はじめの一歩

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

調査の方向性

issue にリンクされている TypeScript Playground の再現コードから始め、optional-property predicate に対する 5.4 と 5.5 の動作を比較してください。関連するコンテキストについて issue #55257 の議論を読み、提供された例で predicate が推論され、optional property がエラーなしで絞り込まれれば完了です。

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

評価

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

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

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