assert-never
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.2.1
    1,427,269
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.2.1
    1,427,269
  • 1.2.0
    5,118
  • 1.1.0
    103
  • 1.0.0
    8

Package Sidebar

Install

npm i assert-never

Weekly Downloads

1,363,509

Version

1.2.1

License

MIT

Unpacked Size

4.17 kB

Total Files

5

Last publish

Collaborators

  • aikoven