UNPKG

@dozerg/no-new

Version:

Convert ES6 class constructor to normal function.

24 lines (23 loc) 527 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = noNew; /** * Return a function to construct a class object without 'new'. * * @example * ``` * class A { * constructor(a: numbber, b: string) {} * } * * const AA = noNew(A); * const a = AA(1, 'abc'); // same as new A(1, 'abc'); * ``` */ function noNew(c) { return new Proxy(c, { apply(target, args, argumentsList) { return new target(...argumentsList); }, }); }