How can I achieve something similar to this pattern in typescript?
class A {
Init(param1: number) {
}
}
class B extends A {
Init(param1: number, param2: string) {
}
}
The code cut above looks as if it should work, but with a closed inspection of How the Typescript function overload function works , an error occurs:
TS2415: 'Class 'B' incorrectly extends base class 'A'.
Types of property 'Init' are incompatible.
I know that constructor functions allow this behavior, but I cannot use constructors here because these objects are combined for memory efficiency.
I could provide another definition of Init () in class A:
class A {
Init(param1: number, param2: string): void;
Init(param1: number) {
}
}
However, this is not as ideal as the base class should now know about all its derived classes.
Init B, , Init() , , Init() .
, ?