feat(dev): add `assertUnreachable`

Asserts that a case is unreachable in `switch` statements (and really
any flow control construct)
This commit is contained in:
Rickard Andersson 2023-08-03 10:53:58 +03:00
parent f36bf56a6a
commit 41e24a1e4f
1 changed files with 18 additions and 0 deletions

18
src/utilities.ts Normal file
View File

@ -0,0 +1,18 @@
/**
* Asserts that a case in a switch statement is unreachable.
*
* @example
* // Exhaustively checks `user.role` type ("admin" | "user") and will not compile
* // if a new role is added without adding a case to the switch statement.
* switch (user.role) {
* case "admin":
* break;
* case "user":
* break;
* default:
* assertUnreachable(user.role);
* }
*/
export function assertUnreachable(value: never): never {
throw new Error(`Unreachable case: ${value}`);
}