-
-
Notifications
You must be signed in to change notification settings - Fork 49k
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
chore: Use unreachable exception in favor of type safety #22933
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add tests to make sure this change works as expected?
break; | ||
default: | ||
console.warn(new UnreachableException(size)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.warn(new UnreachableException(size)); | |
console.warn(new UnreachableException(size)); | |
break; |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 75136e0:
|
Codecov Report
@@ Coverage Diff @@
## master #22933 +/- ##
=======================================
Coverage 98.31% 98.31%
=======================================
Files 363 364 +1
Lines 7253 7258 +5
Branches 1978 1991 +13
=======================================
+ Hits 7131 7136 +5
Misses 122 122
Continue to review full report at Codecov.
|
Could you add test case? |
👌 |
🤔 This is a ...
👻 What's the background?
Switch
is not type safe.e.g.
Which looks good now.
However, if another value is added to the enum, e.g.
type Pet = 'dog' | 'cat' | 'hamster'
.The
getPet
function stay silent in the compile time.It may (or may not) throw an error in the run time which is error prone.
💡 Solution
It is possible to utilize the
never
type in typescript to force the switch statement to behave like an exhaustive pattern match to enhance the type safety.All occurrence will be changed if the gist of this PR is preferred.
📝 Changelog
☑️ Self Check before Merge