Microsoft/TypeScript
在 GitHub 查看Binary expression in an `if` condition can't narrow identifier reassigned within expression of the optional chain
Open
#56,998 创建于 2024年1月9日
Experience EnhancementHelp WantedSuggestion
仓库指标
- Star
- (48,455 star)
- PR 合并指标
- (平均合并 6天 17小时) (30 天内合并 9 个 PR)
描述
🔎 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