2.0.2 • Published 2 years ago

@ryanmorr/typed v2.0.2

Weekly downloads
-
License
Unlicense
Repository
github
Last release
2 years ago

typed

Version Badge License Build Status

Statically typed properties for object literals

Install

Download the CJS, ESM, UMD versions or install via NPM:

npm install @ryanmorr/typed

Usage

Restrict properties to a native data type:

import typed from '@ryanmorr/typed';

const object = typed({
    foo: String,
    bar: Number 
});

object.foo = 'foo';
object.bar = 123;

Constrain a property to a user-defined constructor:

function Foo() {}

const object = typed({
    foo: Foo
});

object.foo = new Foo();

If a value of the wrong type is assigned to a property, a TypeError will be thrown:

const object = typed({
    foo: String
});

object.foo = 123 // throws: Invalid value assignment on "foo", expected: String, actual: Number

License

This project is dedicated to the public domain as described by the Unlicense.

2.0.2

2 years ago

2.0.1

2 years ago