Microsoft/TypeScript
GitHub ã§èŠãBinary expression in an `if` condition can't narrow identifier reassigned within expression of the optional chain
Open
#56,998 opened on 2024幎1æ9æ¥
Experience EnhancementHelp WantedSuggestion
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð 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