Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including @fab1o/type-checking with all npm packages installed. Try it out:

var typeChecking = require("@fab1o/type-checking")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@fab1o/type-checking v1.2.0

Throws Error when data fails to meet params.

@fab1o/type-checking

Throws an Error when data fails to meet params configuration, ensuring data quality, i.e. data is both correct and useful.

npm install @fab1o/type-checking

[Documentation](/Doc.md)

Motivation

  1. Simplification: It makes type-checking clean, declarative, easy to read and maintain;
  2. Performance: Avoids creating the error message before the assertion fails in most cases;
  3. Configurable: Throw your own Error object, and customize the error messages to your liking;
  4. Featureful: Array of types, Optional, Nullable and Undefinable types, Custom types and User-defined types;
  5. Smart error messages enable easier troubleshooting:
    • Dynamic function signature;
    • Base class parameters indicator;
    • Expected type and expected data;
    • Received data;

Example

import { Types, typecheck } from '@fab1o/type-checking';

function setName(name, year) {
    const params = {
        name: Types.string,
        year: Types.number.optional
    };

    typecheck(setName, params, arguments);
}

setName(2020);
// error: setName(name, year) name expected a String but received a Number: 2020

setName('SDK', '2020');
// error: setName(name, year) year expected a Number or null or undefined but received a String: "2020"

setName('SDK', 2020);
// success
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free