I am in the process of converting some of the old ES5 projects to TypeScript. In these projects, there are old-style classes (functions) defined as follows (simplified examples):
function MyClass(arg1) {
this.arg_prop_1 = arg1;
this.arg_prop_2 = function() { return this.arg_prop_1; }
}
MyClass.prototype.proto_prop = function() { return this.arg_prop_2() === 42; }
const obj = new MyClass(42);
console.assert(obj.proto_prop());
What is the correct / recommended / best way to type this code without actually changing it to ES6 class syntax?
What I have tried:
To enable TypeScript, I have defined some declarations as follows (not working):
interface MyClassThis {
arg_prop_1: number;
arg_prop_2: () => number;
proto_prop: () => boolean;
}
interface MyClassCtor {
new (arg1: number): MyClassThis;
}
To begin with, I just followed some code for “Date” type from here. As the above is not working (error: ‘new’ expression, whose target lacks a construct signature, implicitly has an ‘any’ type.(7009)), I’m not completely sure what’s the right way to define types for such old-style class functions.