hype-type
Install
npm i hype-type
Usage
Using hype-type
you can check the type of value
using built-in library types or your own ones
'use strict' const Type = ; TypeDate; // true const integer = TypeNumber && value % 1 === 0; integer; // trueinteger; // false
API
Library exports Type
class with assigned built-in types.
Type class
Type
instances are created by defining type's its constraint,
extending an existing type or wrapping a class:
/** * Simple number type */const number = typeof value === 'number';number; // truenumber; // false /** * Extending an existing type */const integer = Type;integer; // trueinteger; // false /** * Create a type from a class */const map = Type;map; // truemap; // false
Type
methods:
is(value)
- check whether the value accepts type's constraint
Type
static methods:
extend(type, constraint)
- create a new type, inheriting all parent's constraintsfrom(class)
- create a new type, which values are instances ofclass
union(...types)
- create aTypeUnion
instance from giventypes
TypeUnion class
TypeUnion
extends Type
class, so it also has an is
method.
TypeUnion
methods:
of(value)
- return the type ofvalue
, if value accepts some fromTypeUnion
types.
const unionType = Type;unionType; // trueunionType; // trueunionType; // false unionType; // Type.NumberunionType; // Type.StringunionType; // undefined
Schema class
Schema
class is useful for defining object property types
const Type Schema = ; const person = id: TypeNumber name: TypeStirng born: TypeDate; person; // true
Schema
extends Type
class, so it has the same methods.
You can also define optional properties using []
syntax:
const person = id: TypeNumber name: TypeString born: TypeDate // optional; person; // true person; // false, required property `id` is missing