-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Description
Which @angular/* package(s) are relevant/releated to the feature request?
core
Description
I have an interceptor which needs to inject a service. I followed this SO thread and added the required service to the deps
, so I can inject it.
However, I just realized that the types of the injected services in the interceptor and the types of the provided deps
are not checked against. So it can happen that in the deps
, the wrong service is used. But this does neither lead to a build nor to a runtime error. That means, it can easily happen that an interceptor breaks without being noticed.
I've set up a repo for demonstration: https://stackblitz.com/edit/angular-ivy-xgjm7m?file=src%2Fapp%2Fapp.module.ts
When adding the correct service to the provider deps, an additional header will be added to the HTTP request. However, when injecting a wrong service, e.g. HttpClient, the header will not be added, but no error is thrown.
Proposed solution
When using class providers with depency injection, the injected elements in the class constructor and the deps
of the providers should be compared by the framework. In case of a mismatch, an error should be thrown.
Alternatives considered
The type of the injected services can be manually checked:
@Injectable()
export class MyInterceptor implements HttpInterceptor {
constructor(private myService: MyService) {
if (!(this.myService instanceof MyService)) {
throw new Error('expected type MyService');
}
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// ... interceptor logic
return next.handle(request);
}
}