Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

type-assert

2.1.0 • Public • Published

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

Keywords

Install

npm i type-assert

DownloadsWeekly Downloads

195

Version

2.1.0

License

MIT

Last publish

Collaborators

  • avatar