-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exhaustive switch on a type union with a default case yields TS2339: Property 'x' does not exist on type 'never' #9838
Comments
The code above cannot go wrong in TypeScript land, as you have noted. The only reasonable error I can conceive is someone pass a value from the outside of TS or someone naughtily assert In any case, I think your workaround is a good alarm of |
Indeed. Another example is a function called from a HTML template event handler with no type safety in which case you can just have a typo somewhere trigger the default case. This is why I believe TypeScript should allow seemingly unnecessary default cases. |
I dislike this current behavior. This should not be an error. It is quite weird TypeScript could change the type of an object (to |
If you can't trust that the passed in |
Seems wrong -- |
Seems to be fixed in Anders' current literal types PR |
Fixed in #9407. |
TypeScript Version: 2.0.0
Code
Expected behavior:
The code compiles with no errors.
Actual behavior:
tsc test.ts
test.ts(14,53): error TS2339: Property 'type' does not exist on type 'never'.
I understand the compiler knows that with the current code the default case can never be reached and the type property can not have any possible valid TestType value in this case but the whole point of having a default case is to limit the scope of the human error, compiling with a different TS version, compiler flags etc. It can also be considered a safe coding practice.
The workaround is easy but ugly and should not be necessary:
The text was updated successfully, but these errors were encountered: