es7-checktypes-decorator
v0.2.1
A simple es7 decorator to control your class and methods arguments types or instances.
If the decorator detects an anomaly, it will throw a ArgumentTypeError
with a message showing the wrong argument.
Installation
Configure babel to use decorators:
npm i --save-dev babel-transform-decorators-legacy
and add "plugins": ["transform-decorators-legacy"]
in your .babelrc.
then: npm i --save-dev es7-checktypes-decorator
Usage
First, require the decorator:
;
The decorator will control if your arguments have the good type and / or are an instance of a Class.
You have simply to call the decorator before a method or a class. Like that:
@// This class expects to receive arg1 as Number (typeof) and arg2 as CustomClass instance (instanceof). {} @ {} @ {} @ // This method expects to receive arg1 as String or CustomClass instance // and arg2 as Number or String. {}
If the validation is ok, your class or your methods will be executed as always.
If the validation is wrong, the decorator will throw a ArgumentTypeError
(inherited from TypeError) with a custom message.