1.0.0 • Published 3 years ago

@suin/non-exhaustive-error v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

@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-error

Usage

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

https://suin.github.io/non-exhaustive-error/