assert-never

Helper function for exhaustive checks of discriminated unions in TypeScript

MIT 6 个版本
安装
npm install assert-never
yarn add assert-never
pnpm add assert-never
bun add assert-never
README

Assert Never npm version

Helper function for exhaustive checks of discriminated unions in TypeScript.

Installation

npm install --save assert-never

Usage

import {assertNever} from "assert-never";

type A = {type: 'a'};
type B = {type: 'b'};
type Union = A | B;

function doSomething(arg: Union) {
  if (arg.type === 'a') {
    return something;
  }

  if (arg.type === 'b') {
    return somethingElse;
  }

  // TS will error if there are other types in the union
  // Will throw an Error when called at runtime. Use `assertNever(arg, true)`
  // instead to fail silently.
  return assertNever(arg);
}
版本列表
1.4.0 2024-12-17
1.3.0 2024-07-07
1.2.1 2020-05-13
1.2.0 2019-04-24
1.1.0 2017-12-25
1.0.0 2017-03-27