I've been experimenting with using alternative class provider in Angular. To illustrate my issue, let me provide an example scenario. I have a service defined as follows:
export MyService {
method() {
// Some Code
}
}
Now, I am creating a child service by extending the parent service like this:
export MyAnotherService extends MyService {
anotherMethod() {
// Some Code
}
}
Next, I specify the provided services in the app module's configuration in the following manner:
{ provide: MyService, useClass: MyAnotherService }
Then, within my component, I'm injecting the parent service and attempting to call the additional method like so:
constructor(
private service: MyService
) {}
ngOnInit() {
this.service.method();
this.service.anotherMethod(); // <- Typescript complains about this line.
}
While the application functions correctly without any errors in the browser, TypeScript raises a warning:
[ts] Property 'anotherMethod' does not exist on 'MyService'.
Is there a way to notify TypeScript that the anotherMethod
actually exists?
Note: Although accessing this.service['anotherMethod']()
resolves the issue, I am seeking a solution to make TypeScript recognize this method.