feat(dev): add `assertUnreachable`
Asserts that a case is unreachable in `switch` statements (and really any flow control construct)
This commit is contained in:
parent
f36bf56a6a
commit
41e24a1e4f
|
@ -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}`);
|
||||||
|
}
|
Loading…
Reference in New Issue