After switching to Angular 5.2.3, and running ng cli
with a linter rule of "deprecation": true
i get the following error in many of my tests:
get is deprecated: from v4.0.0 use Type<T> or InjectionToken<T>
The line that complains is something like
fixture.debugElement.injector.get(MyService)
with MyService
being a class used for dependency injection.
I am at a loss to guess what the new syntax is supposed to be, as
fixture.debugElement.injector.get<MyService>(MyService)
gives a compile error that Argument of type 'typeof MyService' is not assignable to parameter of type 'Type<MyService> | InjectionToken<MyService>'.
What is the correct way to use the injector to avoid deprecation errors?
Edit: See answer below, for Angular9 update.
Edit: It seems to be related to MyService being an abstract class. If I use a non-abstract class, the .get<T>(T)
syntax works.
Edit: Fix is apparently pending some PR work - https://github.com/angular/angular/pull/25222
MyService
– Doesfixture.debugElement.injector.get<MyService>(Type<MyService>)
– BiblicistType
is constructor function. There should be no problems if MyService is a class. Make sure that imports are correct and so on. – Doterror TS1005: '(' expected.
. Also VSCode says something likecannot find 'Type'
if i dont explicitly import it from @angular/core, and'[ts] Expected 0 type arguments, but got 1
if i do import it. However if i do import it, and ignore VSCode, and run the test, i getError: StaticInjectorError(DynamicTestModule)[anonymous]: StaticInjectorError(Platform: core)[anonymous]: NullInjectorError: No provider for anonymous!
– Generalissimo