Reputation: 2977
I created such a class
@Injectable FooService {
constructor(protected _bar:BarService){
}
}
And extended it like this
@Injectable ExtFooService extends FooService {
constructor(_bar:BarService){
super(_bar);
}
}
But then I extended BarService like this
@Injectable ExtBarService extends BarService {
public myBarVar = "myBarVar";
constructor(){
super();
}
}
And now here
@Injectable ExtFooService extends FooService {
constructor(_bar:ExtBarService){
super(_bar);
}
someMethod(){
return {
key: this._bar.myBarVar
};
}
}
But I get the following error:
Error:(51, 28) TS2551:Property 'myBarVar' does not exist on type 'BarService'.
It's like ExtBarService is forced to BarService because the super class wants that... Is it possible that I have to do this?
@Injectable ExtFooService extends FooService {
constructor(_bar:BarService, private _extBar: ExtBarService){
super(_bar);
}
someMethod(){
return {
key: this._extBar.myBarVar
};
}
}
Upvotes: 0
Views: 408
Reputation: 249656
You can't override a private
field, but you can't access the field from a derived class anyway, so I'm not sure where you get the error, but I'm guessing this is not your complete code.
The simplest solution would be to make the field protected
. It would not be accessible from the outside, it would be accessible form derived classes, and you can override the type with a derived service without errors:
@Injectable class FooService {
constructor(protected _bar:BarService){
}
}
@Injectable class ExtFooService extends FooService {
constructor(protected _bar:ExtBarService){
super(_bar);
}
someMethod(){
return {
key: this._bar.myBarVar
};
}
}
Upvotes: 1