Microsoft/TypeScript
GitHub ã§èŠãCondition aliasing doesn't capture optional chain expressions
Open
#49,075 opened on 2022幎5æ11æ¥
BugDomain: check: Control FlowHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
Bug Report
ð Search Terms
undefined optional chain aliased condition narrowing
ð Version & Regression Information
- This is the behavior in every version I tried (from 4.4 onward, when we added aliased conditions).
⯠Playground Link
ð» Code
interface Foo {
getValues(): number[];
}
declare const foo: Foo | undefined;
const vals = foo?.getValues();
if (vals) {
foo; // Foo | undefined
}
const vals2 = foo && foo.getValues();
if (vals2) {
foo; // Foo
}
if (foo?.getValues()) {
foo; // Foo
}
ð Actual behavior
foo is potentially undefined in the first if block. The assignments fail.
(Added assignments so there's some feedback now that the playground's //^? doesn't work pre-4.6.)
ð Expected behavior
foo is narrowed to Foo in the first if block, matching the narrowing in the other if blocks.