Microsoft/TypeScript
GitHub ã§èŠãAliased optional chain calls don't narrow like their inline equivalents
Open
#59,145 opened on 2024幎7æ5æ¥
Domain: check: Control FlowHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
aliased conditions control flow analysis cfa optional chain call
ð Version & Regression Information
- This is the behavior in every version I tried
⯠Playground Link
ð» Code
function resolve1(id: string | undefined) {
const isBundled = id?.includes("_bundled");
if (isBundled) {
const str: string = id; // error
}
}
function resolve2(id: string | undefined) {
if (id?.includes("_bundled")) {
const str: string = id; // ok
}
}
ð Actual behavior
resolve1 errors within the if block
ð Expected behavior
I'd expect it to be OK. Just like resolve2 is OK with it here - the condition is exactly the same and TS supports Control Flow Analysis of Aliased Conditions and Discriminant since 4.4
Additional information about the issue
No response