is-type-guard
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

is-type-guard

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'),
}

Package Sidebar

Install

npm i is-type-guard

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

3.24 kB

Total Files

4

Last publish

Collaborators

  • rwu823