notmytype provides a type-asserting function that handles multiple arguments and uses the type asserter from flowcheck.
In your project, run:
npm install notmytype --save
or install from the GitHub repo:
npm install ludios/notmytype --save
notmytype requires io.js 2.0.0+/node 4.0.0+ running with --harmony-rest-parameters
.
const T = require('notmytype');
All of these examples type-check without error:
T(3, T.number);
T(3, T.number, "hello", T.string);
T(3, T.number, true, T.boolean);
T([true, false, true], T.list(T.boolean));
T([true, 3, true], T.list(T.union([T.boolean, T.number])));
T(undefined, T.optional(T.number));
T(3, T.optional(T.number));
T(null, T.maybe(T.number));
T(3, T.maybe(T.number));
T(new Buffer("hi"), Buffer);
T([3, "hi"], T.tuple([T.number, T.string]));
T({"hello": 3}, T.dict(T.string, T.number));
T(undefined, T.void);
T({"hello": 3}, T.shape({"hello": T.number}));
T({"hello": 3, "extra": "s"}, T.shape({"hello": T.number}));
T(Symbol('a'), T.symbol);
T()
raises TypeError
if given an object of the wrong type:
> T([3, "s"], T.list(T.number))
TypeError: First object: Expected an instance of number; got "s", context: Array<number> / 1
See assert.js and test/assert.js for details.