Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rwu823/is-type-guard

πŸ’‚is-type-guard - Types checking made easy
https://github.com/rwu823/is-type-guard

guard is ts type typescript

Last synced: about 1 month ago
JSON representation

πŸ’‚is-type-guard - Types checking made easy

Awesome Lists containing this project

README

        


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.

```ts
([]) instanceof Object // true
({}) instanceof Object // true

typeof [] === 'object' //true
typeof {} === 'object' //true
```

## Installation

```sh
$ yarn add is-type-guard
```

## Usage

```ts
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.

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