https://github.com/umbrellio/typecheck
Strict, declarative, extensible runtime type checker for JavaScript
https://github.com/umbrellio/typecheck
check type type-check type-checker typecheck
Last synced: 4 months ago
JSON representation
Strict, declarative, extensible runtime type checker for JavaScript
- Host: GitHub
- URL: https://github.com/umbrellio/typecheck
- Owner: umbrellio
- License: mit
- Created: 2019-09-09T10:15:11.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-04T16:43:37.000Z (almost 3 years ago)
- Last Synced: 2025-12-27T00:52:13.624Z (6 months ago)
- Topics: check, type, type-check, type-checker, typecheck
- Language: TypeScript
- Homepage: https://npm.im/@umbrellio/typecheck
- Size: 90.8 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# typecheck
Simple, strict, extensible runtime type checker for JavaScript.
## Basic usage
```javascript
import T from '@umbrellio/typecheck'
// define a type
const userType = T.Struct({
email: T.String,
password: T.String,
age: T.Option(T.Number),
role: T.Sum('admin', 'support'),
})
// run a type check
T.check(userType, {
email: 'cow@cow.cow',
password: '12345',
age: 12,
role: 'admin',
}) // => true
```
## Installation
Install with yarn:
```sh
$ yarn add @umbrellio/typecheck
# or with npm:
$ npm i -S @umbrellio/typecheck
```
## Built-in type reference
### Primitive types
- T.String: `kek`, `new String('pek')`
- T.Number: `69`, `new Number(100)`, `-2131.31`
- T.Boolean: `true`, `false`
### Arrays
Signature: `T.Array(type)`
```javascript
T.Array(T.String) // [], ['kek', 'pek']
T.Array(T.Sum(T.Boolean, T.Number)) // [], [true, 1]
```
### Option
Signature: `T.Option(type)`
```javascript
T.Option(T.String) // null, undefined, 'some string'
T.Option(T.Array(T.Number)) // null, undefined, [], [69]
```
### Struct
Signature: `T.Struct(schema)` where `schema` is an object
```javascript
T.Struct({ name: T.String }) // { name: 'Ivan' }
T.Struct({ email: T.String, age: T.Option(T.Number) }) // { email: 'ivan@ivan.ru', age: 69 }
```
### Sum
Signature: `T.Sum(...types)`
```javascript
T.Sum(T.Number, T.String, T.Struct({ name: T.String })) // 69, '69', { name: 'ivan' }
```
## Advanced usage
### Custom types
```javascript
import T from '@umbrellio/typecheck'
// age is a positive number
const Age = new T.Type(v => T.check(T.Number, v) && v > 0)
const userType = T.Struct({
email: T.String,
age: T.Option(Age),
})
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/typecheck.
## License
Released under MIT License.
## Authors
Created by Alexander Komarov.