arg-typecheck
v0.1.1
Published
A simple type checker for function arguments in JavaScript.
Downloads
2
Readme
arg-typecheck
A simple type checker for function arguments in JavaScript.
Example
var tc = require('arg-typecheck');
var typecheck = tc.typecheck;
var t = tc.types;
/**
* @param {string} a
* @param {string|number} b
* @param {object} [c] - Optional.
*/
function foo(a, b, c) {
typecheck(arguments, [t.String(), t.Either(t.String(), t.Number()), t.Option(t.Object())]);
return a + b + c;
}
foo('a', 1);
// => 'a1'
foo('a', 'b', {});
// => 'ab[object Object]'
foo();
// TypeCheckError: foo(string,either<string,number>,option<object>) called with ()
foo(1, 1, 1);
// TypeCheckError: foo(string,either<string,number>,option<object>) called with (number,number,number)