Skip to content

Helper function for exhaustive checks of discriminated unions in TypeScript

Notifications You must be signed in to change notification settings

skimhugo/assert-never

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Assert Never npm version

Helper function for exhaustive checks of discriminated unions in TypeScript.

Installation for npm

npm install --save assert-never

Usage for Deno

import {assertNever} from "https://deno.land/x/[email protected]/mod.ts";

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

About

Helper function for exhaustive checks of discriminated unions in TypeScript

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%