https://github.com/aikoven/assert-never
Helper function for exhaustive checks of discriminated unions in TypeScript
https://github.com/aikoven/assert-never
assert discriminated-unions never typescript
Last synced: 20 days ago
JSON representation
Helper function for exhaustive checks of discriminated unions in TypeScript
- Host: GitHub
- URL: https://github.com/aikoven/assert-never
- Owner: aikoven
- License: mit
- Created: 2017-03-27T12:17:24.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2024-12-17T10:12:22.000Z (6 months ago)
- Last Synced: 2025-04-08T16:05:03.231Z (about 2 months ago)
- Topics: assert, discriminated-unions, never, typescript
- Language: TypeScript
- Homepage:
- Size: 165 KB
- Stars: 45
- Watchers: 4
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Assert Never [![npm version][npm-image]][npm-url]
Helper function for [exhaustive checks][exhaustive-checks] of discriminated
unions in TypeScript.## Installation
```
npm install --save assert-never
```## Usage
```ts
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);
}
```[npm-image]: https://badge.fury.io/js/assert-never.svg
[npm-url]: https://badge.fury.io/js/assert-never
[exhaustive-checks]: https://basarat.gitbook.io/typescript/type-system/discriminated-unions#exhaustive-checks