In my TypeScript project, I am attempting to generate or duplicate a child object using a method within the base class. Here is my simplified code setup:
abstract class BaseClass<TCompositionProps> {
protected props: TCompositionProps;
protected cloneProps(): TCompositionProps { return $.extend(true, {}, this.props); } // can be overwritten by child classes
constructor(props: TCompositionProps){
this.props = props;
}
clone(){
const props = this.cloneProps();
return this.constructor(props);
}
}
interface IProps {
someValues: string[];
}
class Child extends BaseClass<IProps>{
constructor(props: IProps){
super(props);
}
}
Now, I am about to instantiate a new object:
const o1 = new Child({someValues: ["This","is","a","test"]};
// get the clone
const clone = o1.clone();
The constructor is called (but it's just the function call), indicating that a new object is not being created. When I use
return Child.prototype.constructor(props)
instead, I am able to create a new object.
So, how can I invoke the constructor of Child
within its base class?
I have also explored this as a potential solution.