Socket
Book a DemoInstallSign in
Socket

@umbrellio/typecheck

Package Overview
Dependencies
Maintainers
3
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@umbrellio/typecheck

Simple, strict, extensible runtime type checker for JavaScript

0.2.1
latest
Source
npmnpm
Version published
Maintainers
3
Created
Source

typecheck

Simple, strict, extensible runtime type checker for JavaScript.

Basic usage

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:

$ 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)

T.Array(T.String) // [], ['kek', 'pek']
T.Array(T.Sum(T.Boolean, T.Number)) // [], [true, 1]

Option

Signature: T.Option(type)

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

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)

T.Sum(T.Number, T.String, T.Struct({ name: T.String })) // 69, '69', { name: 'ivan' }

Advanced usage

Custom types

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.

Supported by Umbrellio

Keywords

type

FAQs

Package last updated on 04 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.