reflectype

0.0.9 • Public • Published

ReflecType

Type support for javascript

.babelrc setup

{
    "presets": [
        "@babel/preset-env"
    ],
    "plugins": [
        ["@babel/plugin-proposal-decorators", { "version": "2022-03" }]
    ]
}

Usage

Type hint to class property

const {type, allowNull, returnType, defaultArgs, paramsType, Void} = require('reflectype');

class A {

    @type(Number)
    accessor id;

    constructor() {


    }

    @paramsType(Number, String)
    @defaultArgs(2, 'foe')
    @returnType(Void)
    print(id, name) {

        console.log(id, name);
    }
}


const obj = new A();

obj.id = 1;

obj.print(3, 'bar');
obj.print();

console.log(obj.id);

// would throw Error 
obj.id = '123';
obj.print('4', 'john');

Implementing interfaces

const {Interface} = require('reflectype/interface');
const {implement} = require('reflectype');

class ILog extends Interface {

    log() {}
}

/**
 * would throw error because this class has not been defined log() method
 */
@implement(ILog)
class SomeClass {

    constructor() {


    }
}

Package Sidebar

Install

npm i reflectype

Weekly Downloads

1

Version

0.0.9

License

MIT

Unpacked Size

75.6 kB

Total Files

66

Last publish

Collaborators

  • tanhuy998