1.0.0 • Published 8 years ago
type-right v1.0.0
TypeRight: minimal dynamic typing library
To see its functionality demonstrated, take a look at the tests.
Installation:
npm install type-rightImporting:
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); // trueEnforcing the types of values
tr.force('abc', tr.PrimitiveString); // ok
tr.force(undefined, tr.PrimitiveString); // TypeErrorIf 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)); // okEnforcing the types of parameters
function dist(x, y) {
tr.force(x, tr.PrimitiveNumber, y, tr.PrimitiveNumber);
return Math.hypot(x, y);
}1.0.0
8 years ago