Microsoft/TypeScript
Voir sur GitHubBinary expression in an `if` condition can't narrow identifier reassigned within expression of the optional chain
Open
#56 998 ouverte le 9 janv. 2024
Experience EnhancementHelp WantedSuggestion
Métriques du dépôt
- Stars
- (48 455 stars)
- Métriques de merge PR
- (Merge moyen 6j 17h) (9 PRs mergées en 30 j)
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