# assert-never

> Helper function for exhaustive checks of discriminated unions in TypeScript

Latest version **1.4.0** (published 2024-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install assert-never
pnpm add assert-never
yarn add assert-never
bun add assert-never
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.0 |
| Published | 2024-12-17 |
| First published | 2017-03-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 45 |
| Author | Daniel Lytkin |
| Maintainers | aikoven |
| Keywords | typescript, discriminated unions, assert, never |

## Links

- npm: https://www.npmjs.com/package/assert-never
- Repository: https://github.com/aikoven/assert-never
- Homepage: https://github.com/aikoven/assert-never#readme
- Issues: https://github.com/aikoven/assert-never/issues
- npm.io page: https://npm.io/package/assert-never

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.4.0 (latest) — 2024-12-17
- 1.3.0 — 2024-07-07
- 1.2.1 — 2020-05-13
- 1.2.0 — 2019-04-24
- 1.1.0 — 2017-12-25
- 1.0.0 — 2017-03-27

## 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

---
_Source: https://npm.io/package/assert-never · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
