https://github.com/ryanmorr/typed
Statically typed properties for object literals
https://github.com/ryanmorr/typed
data javascript object properties statically-typed
Last synced: 4 days ago
JSON representation
Statically typed properties for object literals
- Host: GitHub
- URL: https://github.com/ryanmorr/typed
- Owner: ryanmorr
- License: unlicense
- Created: 2015-05-05T16:55:25.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2024-01-22T18:25:29.000Z (over 2 years ago)
- Last Synced: 2025-03-02T21:32:27.655Z (over 1 year ago)
- Topics: data, javascript, object, properties, statically-typed
- Language: JavaScript
- Homepage:
- Size: 62.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# typed
[![Version Badge][version-image]][project-url]
[![License][license-image]][license-url]
[![Build Status][build-image]][build-url]
> Statically typed properties for object literals
## Install
Download the [CJS](https://github.com/ryanmorr/typed/raw/master/dist/cjs/typed.js), [ESM](https://github.com/ryanmorr/typed/raw/master/dist/esm/typed.js), [UMD](https://github.com/ryanmorr/typed/raw/master/dist/umd/typed.js) versions or install via NPM:
```sh
npm install @ryanmorr/typed
```
## Usage
Restrict properties to a native data type:
```javascript
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:
```javascript
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:
```javascript
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](http://unlicense.org/).
[project-url]: https://github.com/ryanmorr/typed
[version-image]: https://img.shields.io/github/package-json/v/ryanmorr/typed?color=blue&style=flat-square
[build-url]: https://github.com/ryanmorr/typed/actions
[build-image]: https://img.shields.io/github/actions/workflow/status/ryanmorr/typed/node.js.yml?style=flat-square
[license-image]: https://img.shields.io/github/license/ryanmorr/typed?color=blue&style=flat-square
[license-url]: UNLICENSE