Closed
Description
π Search Terms
"switch", "type narrowing"
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing and switch statements
β― Playground Link
π» Code
function foo(arg: string | undefined) {
if (!arg) throw new Error();
switch (true) {
case arg.toUpperCase() === "A": // OK
return "A";
case arg.toUpperCase() === "B": // error TS18048: 'arg' is possibly 'undefined'.
case arg.toUpperCase() === "C": // error TS18048: 'arg' is possibly 'undefined'.
case arg.toUpperCase() === "D": // OK
return "B, C or D";
}
return "Not A, B, C or D";
}
π Actual behavior
In two of the four case statements, the arg
variable has the type string | undefined
instead of string
.
π Expected behavior
I would expect the arg
variable to have the same type in all four case statements.
Additional information about the issue
No response