Switch branches/tags
Nothing to show
Clone or download
rauschma Merge pull request #1 from enko/feature/add-typings
feature(typescript): add typings
Latest commit f95ebe3 Feb 14, 2018

README.md

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);
}