Created
July 27, 2023 18:32
-
-
Save rjz/110822b7765e2479c62d30b3a172c14e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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