type State = "on" | "off";
function checkState(state: State) {
if (state === "on") {
console.log("it's on");
} else if (state === "off") {
console.log("it's off");
} else {
// this would fail to type check if you
// haven't tested all valid `State` values
// in the above if statement:
assertNever<typeof state>();
}
}
Assert that a type is
neverwhen type checking. Throws an unreachable exception if it's reached at runtime. This is meant as an exhaustiveness check.