@typejs/type

1.0.0 • Public • Published

type

A simple function to add runtime type safety to your JavaScript objects.

const Pizza = type({
  price: Number,
  pineapple: Boolean => false
})

new Pizza({ price: '9.99' })
// TypeError: The value for property `price` has an invalid type.

Install

npm install @typejs/type

Use

import { type } from '@typejs/type'

Default values

Specify a default value for a property:

const Pizza = type({
  topping: String => 'cheese'
})

const pizza = new Pizza() 
pizza.topping // 'cheese'

For non-primitive types, set defaults in the constructor

Nullable properties

Make a property nullable by setting null as default value:

const Pizza = type({
  addons: Array => null
})

const pizza = new Pizza() 
pizza.addons // null

Multiple types

Allow multiple types for a property:

const Pizza = type({
  price: [Number, String],
  notes: ([String, Array]) => 'No pineapple'
})

Constructor

Create a constructor to provide any custom initialization. For example to set defaults for class typed properties:

const Pizza = type({
  price: Money,

  constructor ({ discount = 0 }) {
    this.price ??= new Money(9.99 - discount)
  }
})

const pizza = new Pizza({ discount: 2.00 })
pizza.price // Money 7.99

Complete example

const Pizza = type({
  // Primitive type
  name: String,

  // Class type
  price: Money,
  
  // Multiple allowed types
  ingredients: [Array, String],

  // Default value
  topping: Boolean => 'cheese',

  // Nullable value
  addons: Array => null,

  // Default value for class typed properties
  constructor () {
    this.price ??= new Money(9.99)
  }
})

Readme

Keywords

none

Package Sidebar

Install

npm i @typejs/type

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

4.77 kB

Total Files

3

Last publish

Collaborators

  • jpkleemans