Skip to content

Instantly share code, notes, and snippets.

@rjz
Created July 27, 2023 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjz/110822b7765e2479c62d30b3a172c14e to your computer and use it in GitHub Desktop.
Save rjz/110822b7765e2479c62d30b3a172c14e to your computer and use it in GitHub Desktop.
/**
* Represents an error when an unreachable variant is encountered at runtime
*/
export class ExhaustiveCheckError<T> extends TypeError {
public instance: T;
public readonly isUnexpected = true;
constructor(msg: string, instance: T) {
super(msg);
this.instance = instance;
}
}
/**
* Force an exhaustive check in preceding control flow (e.g. `switch` blocks)
*
* See: https://stackoverflow.com/a/39419171
*/
export function assertExhaustiveCheck(x: never): never {
throw new ExhaustiveCheckError('Variation unhandled', x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment