# typebox-assert

> utility function to assert and narrow a type using typebox

Latest version **0.5.0** (published 2024-09-18) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2024-09-18 |
| First published | 2024-05-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stephan Meijer |
| Maintainers | smeijer |
| Keywords | validate, typebox, assert, narrow |

## Links

- npm: https://www.npmjs.com/package/typebox-assert
- Repository: https://github.com/smeijer/typebox-assert
- Homepage: https://github.com/smeijer/typebox-assert#readme
- Issues: https://github.com/smeijer/typebox-assert/issues
- Funding: https://github.com/smeijer/typebox-assert?sponsor=1
- npm.io page: https://npm.io/package/typebox-assert

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.5.0 (latest) — 2024-09-18
- 0.4.0 — 2024-05-15
- 0.3.0 — 2024-05-15
- 0.2.0 — 2024-05-06
- 0.1.0 — 2024-05-06

## README

# typebox-assert

> utility function to assert and narrow a type using typebox

## Install

```sh
npm install typebox-assert
```

## Usage

Note that `assertType` does not just assert, it also:

- removes extra properties from value
- generates missing properties using default schema annotations
- converts type mismatches to their target type where possible

The operations are done on the provided object in a mutable manner.

For best practices, we recommend adding an $id to schemas used by `assertType`.

```ts
import { Type } from '@sinclair/typebox';
import { assertType } from 'typebox-assert';

const Person = Type.Object(
	{
		name: Type.String(),
		email: Type.String(),
	},
	{ $id: 'Person' },
);

function createUser(data: unknown) {
	assertType(Person, data); // throws AssertionError when invalid
	db.users.insert(data); // data is narrowed to type `Person`
}
```

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