You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constassertUnreachable=(x: never): never=>{thrownewError("Unreachable code")}// This is OK for the compilerconstexhaustiveFunctionReturnOK=(value: "a"|"b"): number=>{switch(value){case"a":
return0case"b":
return1}}// This is an ERROR for the compiler:// > Function lacks ending return statement and return type does not include 'undefined'.(2366)constexhaustiveFunctionMissesReturn=(value: "a"|"b"): number=>{switch(value){case"a":
return0case"b":
return1default:
assertUnreachable(value)}}// I think both should be OK instead
🙁 Actual behavior
I have a function with a parameter which is a string union (e.g. value: "a" | "b") and a return of some type (e.g. number). I want to perform an exhaustive switchto decide whatnumberto return based on the value, if it's"a"or"b"`. The compiler correctly identifies that the branches match all the possible values (exhaustive), so it's happy and compiles.
However, if I add a default branch, with an unreachable assert of type never, then the compiler complains that I'm missing a return of type number. This seems weird to me, given that the reason for the assert itself is exactly to ensure that such a branch is actually unreachable, and hence every option of the switch is exhausted.
🙂 Expected behavior
Both behaviours to work and the compiler to understand that a default branch with an unreachable-code assertion is not going to happen and behave like in the case without such a branch, inferring that the only possible return type is number (or whatever type the function is returning).
The text was updated successfully, but these errors were encountered:
As for this comment a workaround is to return the assertUnreachable(x: never), like this:
// Now the compiler doesn't complain anymoreconstexhaustiveFunctionMissesReturn=(value: "a"|"b"): number=>{switch(value){case"a":
return0case"b":
return1default:
returnassertUnreachable(value)// ^ Note this "return" was added}}
Bug Report
🔎 Search Terms
is:issue is:open exhaustive switch
🕗 Version & Regression Information
switch
exhaustiveness and I didn't find anything about this.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
I have a function with a parameter which is a string union (e.g.
value: "a" | "b") and a return of some type (e.g.
number). I want to perform an exhaustive
switchto decide what
numberto return based on the value, if it's
"a"or
"b"`. The compiler correctly identifies that the branches match all the possible values (exhaustive), so it's happy and compiles.However, if I add a
default
branch, with an unreachable assert of typenever
, then the compiler complains that I'm missing a return of typenumber
. This seems weird to me, given that the reason for the assert itself is exactly to ensure that such a branch is actually unreachable, and hence every option of theswitch
is exhausted.🙂 Expected behavior
Both behaviours to work and the compiler to understand that a default branch with an unreachable-code assertion is not going to happen and behave like in the case without such a branch, inferring that the only possible return type is
number
(or whatever type the function is returning).The text was updated successfully, but these errors were encountered: