You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monaxys
1ba7100de7
|
2 years ago | |
---|---|---|
.. | ||
README.md | 2 years ago | |
index.d.ts | 2 years ago | |
index.js | 2 years ago | |
index.ts | 2 years ago | |
package.json | 2 years ago |
README.md
Assert Never
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);
}