typebox-assert
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

typebox-assert

utility function to assert and narrow a type using typebox

Install

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.

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`
}

Package Sidebar

Install

npm i typebox-assert

Weekly Downloads

272

Version

0.4.0

License

MIT

Unpacked Size

5.71 kB

Total Files

7

Last publish

Collaborators

  • smeijer