type-assert

A run-time type assertion library for NodeJS.


Keywords
type, assert
License
MIT
Install
npm install type-assert@2.1.0

Documentation

type-assert Build Status

Type assertion library for NodeJS.

Installation

npm install --save type-assert

Usage

const assert = require('type-assert');

function foo(id, msg, active, age) {
	assert(id).is('Number');
	assert(msg).is('String');
	assert(active).isOptional('Boolean');
	assert(age).is(x => x > 18);
}

try {
	foo(5, 5);
} catch (err) {
	console.error(err.message);
	//=> 5 is not of type String
}

API

is(match)

Throw TypeError if the input is not of type type.

isOptional(match)

Throw TypeError if the input is not of type type, only if the input is provided.

match

Type: string function

The type-check type or a test function that should return true if the assertion is successful.

License

MIT © Sam Verschueren