type-right

1.0.0 • Public • Published

TypeRight: minimal dynamic typing library

To see its functionality demonstrated, take a look at the tests.

Installation:

npm install type-right

Importing:

import * as tr from 'type-right';

Using instanceof for primitive values

Having to choose between typeof and instanceof is annoying. TypeRight uses Symbol.hasInstance to fix this:

console.log('abc' instanceof tr.PrimitiveString); // true
console.log(null instanceof tr.PrimitiveNull); // true

Enforcing the types of values

tr.force('abc', tr.PrimitiveString); // ok
tr.force(undefined, tr.PrimitiveString); // TypeError

If parameters can be missing or undefined:

tr.force('abc', tr.union(tr.PrimitiveString, tr.PrimitiveUndefined)); // ok
tr.force(undefined, tr.union(tr.PrimitiveString, tr.PrimitiveUndefined)); // ok

Enforcing the types of parameters

function dist(x, y) {
    tr.force(x, tr.PrimitiveNumber, y, tr.PrimitiveNumber);
    return Math.hypot(x, y);
}

Dependencies (0)

    Dev Dependencies (4)

    Package Sidebar

    Install

    npm i type-right

    Weekly Downloads

    7

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • rauschma