arg-typecheck

A simple type checker for function arguments in JavaScript.

Usage no npm install needed!

<script type="module">
  import argTypecheck from 'https://cdn.skypack.dev/arg-typecheck';
</script>

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)