Microsoft/TypeScript
View on GitHubBinary expression in an `if` condition can't narrow identifier reassigned within expression of the optional chain
Open
#56,998 opened on Jan 9, 2024
Experience EnhancementHelp WantedSuggestion
Repository metrics
- Stars
- (48,455 stars)
- PR merge metrics
- (Avg merge 6d 17h) (9 merged PRs in 30d)
Description
🔎 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