Microsoft/TypeScript
GitHub ã§èŠãType narrowing in one case statement does not propagate to the next case
Open
#56,361 opened on 2023幎11æ10æ¥
Domain: check: Control FlowHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
ð Search Terms
- Type narrowing
- Switch statement
ð Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about switch statements and type narrowing.
⯠Playground Link
ð» Code
function foo(arg: string | undefined) {
switch (true) {
case arg === undefined:
throw new Error();
case arg.startsWith("x"): // error TS18048: 'arg' is possibly 'undefined'.
return "A";
default:
return "B";
}
}
ð Actual behavior
error TS18048: 'arg' is possibly 'undefined'. in the second case.
ð Expected behavior
I expect the type to be narrowed to string in the second case statement.
Additional information about the issue
See also #56352. Ping @Andarist.