-
Notifications
You must be signed in to change notification settings - Fork 1
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: add exhaustiveness error utility #478
Conversation
We have a few exhaustiveness checks. I think it's time to DRY these out into a simple utility function. (I plan to use this in an upcoming change, which is why I'm doing this now.)
tests/utils.js
Outdated
test('exhaustivenessError', () => { | ||
const bools = [true, false] | ||
bools.forEach((bool) => { | ||
switch (bool) { | ||
case true: | ||
case false: | ||
break | ||
default: | ||
throw exhaustivenessError(bool) | ||
} | ||
}) | ||
}) |
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.
This test doesn't have any assertions (it just makes sure things don't throw unexpectedly). Not sure if there's a cleaner way to test this.
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.
you can use t.execution()
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.
Thanks! Done in c83a120.
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.
Couple of non-blocking ideas/suggestions but otherwise fine with the addition
src/utils.js
Outdated
export const exhaustivenessError = (value) => | ||
new Error(`Exhaustiveness check failed. ${value} should be impossible`) |
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.
thoughts on an implementation that extends Error
instead? e.g.
class ExhaustivenessError extends Error {
constructor(value) {
super(`Exhaustiveness check failed. ${value} should be impossible`)
this.name = 'ExhaustivenessError'
}
}
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.
I think I prefer that. Done in e088115.
tests/utils.js
Outdated
test('exhaustivenessError', () => { | ||
const bools = [true, false] | ||
bools.forEach((bool) => { | ||
switch (bool) { | ||
case true: | ||
case false: | ||
break | ||
default: | ||
throw exhaustivenessError(bool) | ||
} | ||
}) | ||
}) |
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.
you can use t.execution()
The purpose of these exhaustiveness checks is to get a typescript error if we are not checking all the options. Does this achieve the same thing? |
If it does then this looks good to me. |
Yup! Exact same thing. |
We have a few exhaustiveness checks. I think it's time to DRY these out into a simple utility function.
(I plan to use this in an upcoming change, which is why I'm doing this now.)