-
-
Notifications
You must be signed in to change notification settings - Fork 2.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
[switch-exhaustiveness-check] Require default case if discriminant type is not a union #2959
Comments
As it stands right now - this rule only checks union types.
So adding a Adding a I'm not sure if this is a good addition to this lint rule. |
Thanks for the response @bradzacher. I think that's totally fair, and I don't want to overcomplicate this process or rule by enforcing my opinions with a breaking change. I basically just want to implement some solution for codebases that do want to enforce Is there a way to recognize whether the |
No. Lint rules are all isolated. They have no way to tell if other rules are enabled. Happy to have this behind a default off option! |
Just a work-around I've found which works for me: If you are the type of guy who wants this feature, it might be worth thinking about an exhaustive matching guard (https://medium.com/technogise/type-safe-and-exhaustive-switch-statements-aka-pattern-matching-in-typescript-e3febd433a7a#be77). This way, you keep enum Direction {
Up,
Down,
Left,
Right,
}
(myVar: Direction) => {
switch (myVar) {
case Direction.Up:
break;
case Direction.Down:
break;
case Direction.Left:
break;
case Direction.Right:
break;
default:
((_: never) => { })(myVar)
}
} |
I'll have a look on this. |
Repro
or
Expected Result
I would expect to see an error message compatible with the
default-case
ESLint rule, indicating that the switch-exhaustiveness-check rule is invalidated because the expression is not a union type.Actual Result
No error message.
Additional Info
Essentially, I think the best of both worlds is to just have the predictable functionality of the
default-case
rule operate normally except in the event that a switch statement's expression is a union type. Today, there doesn't seem to be any way for the two rules to operate in tandem. Adding this functionality might be a breaking change, but I'm honestly a bit curious how controversial it would be.The text was updated successfully, but these errors were encountered: