Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
get is deprecated: from v4.0.0 use Type<T> or InjectionToken<T>

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

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

A better you can try to convert your service as Type and then use injector like this:

import { Type } from '@angular/core';

fixture.debugElement.injector.get<MyService>(MyService as Type<MyService>)
over 4 years ago · Santiago Trujillo Relatório

0

UPDATE: For Angular9, the syntax has changed to this:

TestBed.inject<IDataService>(IDataService).

This also works for abstract classes.

In VSCode, you can fix most of your cases by this

FIND: TestBed.get\(([^\)]+)\) as .*;
REPLACE: TestBed.inject<$1>($1);

PRE Angular 9. So, it seems to be related to Abstract vs non-abstract classes.

I have posted my findings here: https://github.com/angular/angular/issues/22063 in case someone runs into the same issue.

over 4 years ago · Santiago Trujillo Relatório

0

As a temporary fix you can try following:

fixture.debugElement.injector.get<MyService>(MyService as any)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda