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

Improve exhaustiveness checks #915

Open
jubnzv opened this issue Oct 6, 2024 · 1 comment · May be fixed by #991
Open

Improve exhaustiveness checks #915

jubnzv opened this issue Oct 6, 2024 · 1 comment · May be fixed by #991
Assignees
Labels
enhancement New feature or request refactoring

Comments

@jubnzv
Copy link
Member

jubnzv commented Oct 6, 2024

We could improve compile-time checks provided by the TypeScript compiler introducing the unreachable function:

export function unreachable(value: never): never {
  throw InternalException.make(`Reached impossible case`, { node: value });
}

It accepts the parameter never, so it could be used to perform a static exhaustiveness checks for switch clauses:

switch (node.kind) {
  case "id":
    // ...
  // other cases
  default:
    unreachable(node);
}

If some of the node kinds are not covered by the case clauses, we'll get a compilation error:
picture

That's quite useful especially to avoid regressions when changing enum types implementations.

@jubnzv jubnzv added the enhancement New feature or request label Oct 6, 2024
@jubnzv jubnzv changed the title Improve static exhaustiveness analysis Improve exhaustiveness checks Oct 6, 2024
@verytactical
Copy link
Contributor

verytactical commented Oct 28, 2024

That's what TypeScript devs do in its compiler, so it seems to be as close to a recommended approach as it gets.

I looked up how they intend to use it, and results were quite inconclusive: they use it standalone, with return and even with throw in front.

EDIT. There's a lot of other shiny functions in that file too...

@verytactical verytactical linked a pull request Oct 28, 2024 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactoring
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants