API reference › @evolu/common › Function › exhaustiveCheck
function exhaustiveCheck(value: never): never;
Defined in: packages/common/src/Function.ts:95
Helper function to ensure exhaustive matching in a switch statement. Throws an error if an unhandled case is encountered.
Example
import { exhaustiveCheck } from "@evolu/common";
type Color = "red" | "green" | "blue";
const handled: Array<string> = [];
const handleColor = (color: Color): void => {
switch (color) {
case "red":
handled.push("Handling red");
break;
case "green":
handled.push("Handling green");
break;
case "blue":
handled.push("Handling blue");
break;
default:
exhaustiveCheck(color);
}
};
handleColor("blue");
expect(handled).toEqual(["Handling blue"]);
Use this primarily in side-effect switches (void branches). For
value-producing switches, TypeScript can enforce exhaustiveness without a
default branch in either of the following styles.
Assign in every case
type Input =
| { readonly type: "Mutate" }
| { readonly type: "Query" }
| { readonly type: "Export" };
const inputToKind = (input: Input): "A" | "B" | "C" => {
let result: "A" | "B" | "C";
switch (input.type) {
case "Mutate":
result = "A";
break;
case "Query":
result = "B";
break;
case "Export":
result = "C";
break;
}
return result;
};
expect(inputToKind({ type: "Query" })).toBe("B");