aikoven/assert-never

Helper function for exhaustive checks of discriminated unions in TypeScript

TypeScript

45

18 commits

updated Dec 17, 2024

See the code

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);
}
assert
discriminated-unions
never
typescript

aikoven/assert-never

Helper function for exhaustive checks of discriminated unions in TypeScript

TypeScript

45

18 commits

updated Dec 17, 2024

See the code

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);
}
assert
discriminated-unions
never
typescript

Project health

Overall

2.1/10

Maintained

0/10

Code review

1/10
OpenSSF Scorecard

Latest release

1.4.0 · Dec 17, 2024

Releases this year

0

Significant stargazers

Jed Fox

480 followers · starred Jun 2019

Simon Ihmig

87 followers · starred Apr 2021

Francois Wouts

232 followers · starred May 2021