Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/rwu823/is-type-guard
- Owner: rwu823
- Created: 2018-10-13T15:25:26.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T05:38:18.000Z (about 1 year ago)
- Last Synced: 2024-04-09T22:53:41.890Z (9 months ago)
- Topics: guard, is, ts, type, typescript
- Language: TypeScript
- Homepage:
- Size: 68.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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 // truetypeof [] === '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'),
}
```