diff --git a/src/utilities.ts b/src/utilities.ts new file mode 100644 index 00000000..5ab5221e --- /dev/null +++ b/src/utilities.ts @@ -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}`); +}