@suin/non-exhaustive-error
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i @suin/non-exhaustive-error

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

6.29 kB

Total Files

9

Last publish

Collaborators

  • suin