From b8dfe70870280f21b260b063f245e7675d2bd560 Mon Sep 17 00:00:00 2001 From: roug3-Identy Date: Tue, 16 Jan 2024 13:20:59 +0530 Subject: [PATCH] Fix "Class constructor XXX cannot be invoked without 'new'" when in ESM5 js in Vuew and React. --- src/decorators/auto-injectable.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/decorators/auto-injectable.ts b/src/decorators/auto-injectable.ts index 308b164..993aa98 100644 --- a/src/decorators/auto-injectable.ts +++ b/src/decorators/auto-injectable.ts @@ -6,6 +6,7 @@ import { isTransformDescriptor } from "../providers/injection-token"; import {formatErrorCtor} from "../error-helpers"; +import {__extends} from "tslib"; /** * Class decorator factory that replaces the decorated class' constructor with @@ -18,10 +19,16 @@ import {formatErrorCtor} from "../error-helpers"; function autoInjectable(): (target: constructor) => any { return function(target: constructor): constructor { const paramInfo = getParamInfo(target); - - return class extends target { - constructor(...args: any[]) { - super( + return (function(_super): any { + function extendedClazz(...args: any[]) { + const SuperProxy = new Proxy(_super, { + // target = Foo + apply(target, _, argumentsList) { + return new target(...argumentsList); + } + }); + return SuperProxy.call( + null, ...args.concat( paramInfo.slice(args.length).map((type, index) => { try { @@ -62,7 +69,9 @@ function autoInjectable(): (target: constructor) => any { ) ); } - }; + __extends(extendedClazz, _super); + return extendedClazz; + })(target); }; }