is-type-guard
v1.0.0
Published
Types checking made easy.
Downloads
5
Readme
Why
In TypeScript you can only use typeof
or instanceof
as types checking. It gets a little bit trouble if you want to check a plain object.
e.g.
([]) instanceof Object // true
({}) instanceof Object // true
typeof [] === 'object' //true
typeof {} === 'object' //true
Installation
$ yarn add is-type-guard
Usage
import is from 'is-type-guard'
const foo = 'bar'
if(is.string(foo)) {
}
API
is.string
is.function
is.object
is.array
is.number
is.null
is.undefined
is.document
is.process
ofType
It's based on Object.prototype.toString
Custom types
Easy to extend as your custom types guard.
import isType, { ofType } from './is-type-guard'
const is = {
...isType,
date: (o: any): o is Date => ofType(o, 'Date'),
body: (o: any): o is HTMLBodyElement => ofType(o, 'HTMLBodyElement'),
}