Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

mindscreen/mindscreen-js-decorators

Repository files navigation

npm version Build Status Maintainability

mindscreen JavaScript decorators

A package to gather JS decorators from projects at mindscreen.

TypeGuard

Specify expected argument types on class methods to ensure correct types when calling them. Throws a TypeError if called with an incorrect type or a ReferenceError, if a required argument is missing.

// import { TypeGuard } from 'mindscreen-decorators';
// const { TypeGuard } = require('mindscreen-decorators');

class A {}
class B extends A {}
class C {}

class Example {

    @TypeGuard({
        a: A.prototype,
        b: { required: true, type: 'int' },
        c: 'boolean'
    })
    foo(a, b, c, d) {
        // ...
    }

    @TypeGuard({
        value: 'string'
    })
    set bar(value) {
        // ...
    }
}

const a = new A();
const b = new B();
const c = new C();
const e = new Example();

e.foo(a, 12); // OK
e.foo(a); // ReferenceError: The property 'b' on method 'Example::foo' is required and not set.
e.foo(c, 12); // TypeError: The property 'a' on method 'Example::foo' should be of type 'A', got C.
e.foo(b, 12); // OK

Note that not all arguments have to be mentioned in the @TypeGuard options. Arguments not explicitly configured to be required, won't act this way. Arguments can be set to be required without configuring a type. Alternatively any can be used.

The spread-operator (...args) can't be configured, as it is removed by transpiling your source.

About

JavaScript decorators originated in mindscreen projects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published