Hi,
How does DI work in inheritance?
Ie. let’s take this example (I use Typescript):
@autoinject
export abstract class HttpServiceBase {
constructor (protected httpClient: HttpClient) {
}
}
@autoinject
export class UserService extends HttpServiceBase {
public getAllUsers():Promise<UserModel> {
return this.httpClinet.fetch(....) ///implementation details are not important here
}
}
and the usage:
export class NewUserVM {
constructor (private service: UserService) {}
async activate() {
this.model = await this.service.getAllUsers();
}
}
This throws an exception at run-time when I try to use httpClient, because I forgot to write the constructor in the derived class, so the HttpClient is not being injected.
I know that in Typescript, if the derived class doesn’t specify constructor, the constructor is inherited.
- So why does it not work?
- Also, how can I make all my service instance that derive from HttpServiceBase transient when i use @autoinject?
- Can I somehow enforce the ts-compiler/vscode-ide to detect missing constructor implementation in derived class and throw an error at compile-time, ts-lint maybe?
Thanks