microsoft / microsoft/TypeScript
Binary expression in an `if` condition can't narrow identifier reassigned within expression of the optional chain
オープン
まだ誰も着手していません。
Experience Enhancement
Help Wanted
Suggestion
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
binary condition refinement predicate optional chain undefined
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
type Type = {
id: number;
};
type InferenceInfo = {
typeParameter: Type;
impliedArity?: number;
};
declare function getInferenceInfoForType(type: Type): InferenceInfo | undefined;
function fn1(t1: Type, t2: Type) {
let info = getInferenceInfoForType(t1);
if (info?.impliedArity !== undefined) {
info.impliedArity; // ok
} else if ((info = getInferenceInfoForType(t2))?.impliedArity !== undefined) {
info.impliedArity; // 'info' is possibly 'undefined'.(18048)
}
}
function fn2(t1: Type, t2: Type) {
let info = getInferenceInfoForType(t1);
if (info?.impliedArity !== undefined) {
info.impliedArity; // ok
} else if ((info = getInferenceInfoForType(t2))?.impliedArity) {
info.impliedArity; // ok
}
}
🙁 Actual behavior
The second branch in fn1 contains an error
🙂 Expected behavior
I'd expect this to be narrowed just fine - like the first branch and the second branch in fn2
Additional information about the issue
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground の再現コードから始め、fn1 と fn2 の narrowing の挙動を比較します。特に、再代入された optional chaining 式に注目してください。2 番目の分岐の型チェックの経路を追跡します。fn1 が info.impliedArity へのアクセスをエラーなしで受け入れ、既存のチェックを維持できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100