Assuming I have a 'base' class such as this:
class CcDefinition {
// Some properties here
constructor (json: string);
constructor (someVar: number, someOtherVar: string);
constructor (jsonOrSomeVar: any, someOtherVar?: string) {
if (typeof jsonOrSomeVar=== "string") {
// some JSON wrangling code here
} else {
// assign someVar and someOtherVar to the properties
}
}
}
I want to be able to extend this base class while still supporting constructor overloading. For example:
class CcDerived extends CcDefinition {
// Some additional properties here
constructor (json: string);
constructor (someVar: boolean, someOtherVar: number, someAdditionalVar: string);
constructor (jsonOrSomeVar: any, someOtherVar?: number, someAdditionalVar?: string) {
if (typeof jsonOrSomeVar=== "string") {
super.constructFromJson(jsonOrSomeVar);
} else {
super.constructFromDef(someOtherVar, someAdditionalVar);
// assign someVar to the additional properties of this derived class
}
}
}
The problem is that Typescript demands that the 'super' keyword appear first (literally) in the constructor implementation. The specific build error message is:
"A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties."
However, I need to determine which parameters I will pass into the 'super' (i.e. use a different constructor overload) based upon what was supplied to the extended (derived) class. You should assume here that the derived class' constructor overloads may be very different from the super's.
Is there a workaround for what I'm trying to achieve?