Angular 2 OpaqueToken vs Angular 4 InjectionToken
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'. }}