David answer is great, but if you care just about quickly making it compile (for example because you are migrating from JS to TS) then you can simply put any
there to shut up complaining compiler.
TS file:
const TestConstructorFunction = function (this: any, a: any, b: any) {
this.a = a;
this.b = b;
};
let test1 = new (TestConstructorFunction as any)(1, 2);
compiles to this JS file:
var TestConstructor = function (a, b) {
this.a = a;
this.b = b;
};
var test1 = new TestConstructor(1, 2);
Just pay attention to not make this mistake:
TS file:
// wrong!
let test2 = new (TestConstructorFunction(1, 2) as any);
JS result:
// wrong!
var test2 = new (TestConstructor(1, 2));
and this is wrong. You'll get TypeError: TestConstructor(...) is not a constructor
error at runtime.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…