microsoft / microsoft/TypeScript
Type predicate for an optional property is not inferred
まだ誰も着手していません。
- 主要言語
- 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
💻 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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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