# common-expectations

> Typescript runtime assertions

Latest version **1.0.1** (published 2022-07-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install common-expectations
pnpm add common-expectations
yarn add common-expectations
bun add common-expectations
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-07-28 |
| First published | 2022-07-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.0 |
| Dependencies | 0 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Benjamin Forster |
| Maintainers | e-e-e |
| Keywords | typescript, assertions, expectations, utilities |

## Links

- npm: https://www.npmjs.com/package/common-expectations
- Repository: https://github.com/cmmn-codes/common-expectations
- Issues: https://github.com/cmmn-codes/common-expectations/issues
- npm.io page: https://npm.io/package/common-expectations

## 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.0.1 (latest) — 2022-07-28
- 1.0.0 — 2022-07-28

## README

# Common Expectations

A small collection of common typescript functions for asserting and guarding types.

## Installation

```shell
# using npm
npm install common-expectations
# or using yarn
yarn add common-expectations
```

## Usages

#### `exists<T>(value: T, message: string): void`

A utility for asserting existence of a value when the type may be `undefined` or `null`.

```ts
import { exists } from "common-expectations";

// where use params returns a partial type where `id` maybe undefined.
const { id } = useParams<{ id: string }>();
exists(value, 'Expected id to be defined.')

```

This is intended only to be used in those rare cases where you know based on context that a value
exists but the type system is unable to infer the fact.
A good example of this is when using params in React Router.

#### `isUnreachable(value: never, message?: string): void`

A utility for helping check type exhaustiveness.
It will raise typescript type error when a values type is anything other than `never`,
and will throw an error at runtime if a value is encountered.

```ts
import { isUnreachable } from "common-expectations";

type AnimalType = 'cat' | 'dog' | 'monkey';

function doSomthing(type: AnimalType) {
  switch (type) {
    case "cat":
    case "dog":
    case "monkey":
      break;
    default:
      isUnreachable(type, `Encountered unexpected animal type: ${type}`);
  }
}
```

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