qwqdanchun.github.io/node_modules/assert-never
qwqdanchun 1b6a0fbb18 test 2022-11-11 19:16:46 +08:00
..
README.md test 2022-11-11 19:16:46 +08:00
index.d.ts test 2022-11-11 19:16:46 +08:00
index.js test 2022-11-11 19:16:46 +08:00
index.ts test 2022-11-11 19:16:46 +08:00
package.json test 2022-11-11 19:16:46 +08:00

README.md

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);
}