ES6 Proxy.apply method serves a way to use classes without a "new" keyword.
But on TypeScript, it gives an error "Value of type 'typeof ClassName' is not callable. Did you mean to include 'new'?".
Is there any way to prevent TypeScript errors?
Here is the simple example of it;
class ClassName {
constructor(anyThing?: any) {
if (anyThing) {
// Do something
}
}
}
// ES6 Proxy
const CustomName = new Proxy(ClassName, {
apply: (Target, _thisArg, argumentsList) => {
return new Target(...argumentsList);
}
});
// Test ---------
new ClassName("test"); // return ClassName { }
// No error
new CustomName("test"); // return ClassName { }
// Javascript: no error
// TypeScript: error => Value of type 'typeof ClassName' is not callable. Did you mean to include 'new'?
CustomName("test"); // return ClassName { }