@ryanmorr/typed
v2.0.2
Published
Statically typed properties for object literals
Downloads
3
Maintainers
Readme
typed
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.