1.0.0 • Published 5 years ago
@suin/non-exhaustive-error v1.0.0
@suin/non-exhaustive-error
An error class to detect non-exhaustive if/switch statements in TypeScript.
Installation
yarn add @suin/non-exhaustive-error
# or
npm install @suin/non-exhaustive-errorUsage
Basic Usage
Use NonExhaustiveError in default case of switch statement.
import { NonExhaustiveError } from "@suin/non-exhaustive-error";
type Status = "good" | "normal" | "bad";
function statusToNumber(status: Status): number {
switch (status) {
case "good":
return 1;
case "normal":
return 0;
default:
// this switch statement is non-exhaustive as here is no case statement for "bad".
throw new NonExhaustiveError(status); // You will get a compile error here by TypeScript compiler check.
}
}API Reference
1.0.0
5 years ago