Skip to content
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

Merged
merged 5 commits into from
Feb 15, 2024
Merged

chore: add exhaustiveness error utility #478

merged 5 commits into from
Feb 15, 2024

Conversation

EvanHahn
Copy link
Contributor

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.)

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
Comment on lines 10 to 21
test('exhaustivenessError', () => {
const bools = [true, false]
bools.forEach((bool) => {
switch (bool) {
case true:
case false:
break
default:
throw exhaustivenessError(bool)
}
})
})
Copy link
Contributor Author

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.

Copy link
Member

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()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done in c83a120.

Copy link
Member

@achou11 achou11 left a 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
Comment on lines 73 to 74
export const exhaustivenessError = (value) =>
new Error(`Exhaustiveness check failed. ${value} should be impossible`)
Copy link
Member

@achou11 achou11 Feb 14, 2024

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'
  }
}

Copy link
Contributor Author

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
Comment on lines 10 to 21
test('exhaustivenessError', () => {
const bools = [true, false]
bools.forEach((bool) => {
switch (bool) {
case true:
case false:
break
default:
throw exhaustivenessError(bool)
}
})
})
Copy link
Member

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()

@gmaclennan
Copy link
Member

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?

@gmaclennan
Copy link
Member

If it does then this looks good to me.

@EvanHahn
Copy link
Contributor Author

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?

Yup! Exact same thing.

@EvanHahn EvanHahn merged commit 0d2a17a into main Feb 15, 2024
7 checks passed
@EvanHahn EvanHahn deleted the exhaustivenessError branch February 15, 2024 01:29
@EvanHahn EvanHahn changed the title chore: add exhaustivness error utility chore: add exhaustiveness error utility Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants