Currently, I am in the process of migrating some legacy ES5 projects to TypeScript. These projects contain older-style classes (functions) defined as shown in the simplified examples below:
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());
I am seeking advice on the correct / recommended / best way to provide typings for this code without converting it into ES6 class syntax.
My Attempts So Far:
In order to implement TypeScript, I have declared certain interfaces like this (not working):
interface MyClassThis {
arg_prop_1: number;
arg_prop_2: () => number;
proto_prop: () => boolean;
}
interface MyClassCtor {
new (arg1: number): MyClassThis;
}
To start off, I tried using existing type definitions for the "Date" type from here. Since the above approach is not working (error: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.(7009)), I am still uncertain about the correct method to define types for these old-style class functions.