tcomb-decorators

es7 decorators for tcomb

Usage no npm install needed!

<script type="module">
  import tcombDecorators from 'https://cdn.skypack.dev/tcomb-decorators';
</script>

README

tcomb-decorators

ES7 Decorators for tcomb.

Installation

npm install tcomb-decorators --save

Usage

@func

import { func } from 'tcomb-decorators';
import { String as Str } from 'tcomb';

class SomeClass {
    @func([Str], Str)
    someFunc(value) {
        return value + 'World';
    }
}

const instance = new SomeClass();

instance.someFunc('Hello '); // Works
instance.someFunc(null); // Throws error

@struct

Experimental: The current behaviour maybe changed in the future.

import { struct } from 'tcomb-decorators';
import { String as Str } from 'tcomb';

@struct({
    name: Str
})
class Person {
    constructor(name) {
        this.name = name;
    }
}

new Person('John Doe') // Works
new Person(); // Throws error

The struct decorator works by wrapping the constructor and using tcomb-validations validate function after class construction. So typ checking is only applyed on initialtion. Further modifications of the object does not lead to an validation again.

LISENCE

MIT