Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
Angular 2 OpaqueToken vs Angular 4 InjectionToken

InjectionToken was introduced in Angular 4 and OpaqueToken was marked as deprecated.

According to the manual, it is supposed to be used as

const anyToken = new InjectionToken('any');

for untyped token, and as

const numberToken = new InjectionToken<number>('number');

for typed token.

However, typed token still can be injected and used with different type when it is injected, TypeScript will be ok with this, won't it?

constructor(@Inject(numberToken) any, @Inject(numberToken) string: string) { ... }

How is InjectionToken supposed to benefit from TypeScript type system?

Why was OpaqueToken deprecated if there's no practical difference between those two?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Based on the internal usage of InjectionToken, for example, here, I assume that InjectionToken gives you type checking benefit when getting a dependency through injector instance:

import {Component, InjectionToken, Injector} from "@angular/core";

interface AppConfig {
    name: string;
}

let APP_CONFIG = new InjectionToken<AppConfig>('app.config');
let appConfig: AppConfig = {name: 'Cfg'};

@Component({
    ...
    providers: [{provide: APP_CONFIG, useValue: appConfig}]
})
export class TestComponent {
    constructor(injector: Injector) {
        const config = injector.get(APP_CONFIG);
        config.s = 'd';
            ^^^^^ - Error:(14, 16) TS2339:Property 's' does not exist on type 'AppConfig'.
    }
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda