Looking for ways to instantiate classes without new in typescript, this fine answer https://stackoverflow.com/a/44061744/430531 works nicely in javascript.
The code in the repo referred https://github.com/digital-flowers/classy-decorator is a simple class decorator:
var classy = function classy() {
return function (Class) {
var _Class = function _Class() {
for (var _len = arguments.length, rest = Array(_len), _key = 0; _key < _len; _key++) {
rest[_key] = arguments[_key];
}
return new(Function.prototype.bind.apply(Class, [null].concat(rest)))();
};
_Class.prototype = Class.prototype;
return _Class;
};
};
But using it under Typescript like this:
@classy()
class Someone{
constructor(private name: string){}
}
debugger
let some1 = new Someone("john");
let some2 = Someone("joe") // some2 is also an instance of Someone! but typescript complains that it's not callable
I have yet to dig into the function decorator itself to understand it well and that also has ts errors but is operational.
But I wonder if there's a way to avoid the Typescript error when instancing without new (let some2 = Someone("joe")
)