Microsoft/TypeScript
Auf GitHub ansehenType narrowing in one case statement does not propagate to the next case
Open
#56.361 geöffnet am 10. Nov. 2023
Domain: check: Control FlowHelp WantedPossible Improvement
Repository-Metriken
- Stars
- (48.455 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)
Beschreibung
🔎 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.