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

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.5.0
    177
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.5.0
    177
  • 0.4.0
    318
  • 0.3.0
    1
  • 0.2.0
    1
  • 0.1.0
    2

Package Sidebar

Install

npm i typebox-assert

Weekly Downloads

499

Version

0.5.0

License

MIT

Unpacked Size

8.01 kB

Total Files

9

Last publish

Collaborators

  • smeijer