Reputation: 4211
I want to do this:
export abstract class Base{
constructor(){
this.activate();
}
protected abstract activate():void;
}
class MyClass extends Base{
static $inject = ['myService'];
constructor(service: myService){
super();
this.myService = myService;
}
activate():void{
this.myService.doSomething();
}
}
But I can't because type of 'this' in derived class method is of 'Base'. How can i make my code work?
Please help. Thanks
Upvotes: 0
Views: 861
Reputation: 28141
The problem is that, the moment activate()
gets called, this.myService
is not yet set.
This is the callstack:
MyClass::constructor() -> super() -> Base::constructor() -> MyClass::activate()
So in the constructor of MyClass
, you'll need to assign this.myService
before calling the base constructor:
class MyClass extends Base{
static $inject = ['myService'];
constructor(service: myService){
this.myService = myService; // <-- before super();
super();
}
activate():void{
this.myService.doSomething();
}
}
Upvotes: 3