We have the following structure
abstract class testParent{
constructor(){
console.log(this.doSomething());
}
abstract doSomething():string;
}
class testChild extends testParent{
private x:string = 'someValue';
constructor(){
super();
}
doSomething():string{
return this.x;
}
}
const y = new testChild();
And we noticed that the string x is not yet initialized so it returns undefined. But we would like to have this structure. What are some ways to deal with this problem?
We thought of one solution
abstract class testParent{
constructor(){
this.initialize();
console.log(this.doSomething());
}
abstract initialize():void;
abstract doSomething():string;
}
class testChild extends testParent{
private x:string = '';
initialize():void{
this.x = 'someValue';
}
doSomething():string{
return this.x;
}
}
const y = new testChild();
But this feels quite hacky.