feat(core): Add type information to injector.get() (#13785)
- Introduce `InjectionToken<T>` which is a parameterized and type-safe version of `OpaqueToken`. DEPRECATION: - `OpaqueToken` is now deprecated, use `InjectionToken<T>` instead. - `Injector.get(token: any, notFoundValue?: any): any` is now deprecated use the same method which is now overloaded as `Injector.get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T;`. Migration - Replace `OpaqueToken` with `InjectionToken<?>` and parameterize it. - Migrate your code to only use `Type<?>` or `InjectionToken<?>` as injection tokens. Using other tokens will not be supported in the future. BREAKING CHANGE: - Because `injector.get()` is now parameterize it is possible that code which used to work no longer type checks. Example would be if one injects `Foo` but configures it as `{provide: Foo, useClass: MockFoo}`. The injection instance will be that of `MockFoo` but the type will be `Foo` instead of `any` as in the past. This means that it was possible to call a method on `MockFoo` in the past which now will fail type check. See this example: ``` class Foo {} class MockFoo extends Foo { setupMock(); } var PROVIDERS = [ {provide: Foo, useClass: MockFoo} ]; ... function myTest(injector: Injector) { var foo = injector.get(Foo); // This line used to work since `foo` used to be `any` before this // change, it will now be `Foo`, and `Foo` does not have `setUpMock()`. // The fix is to downcast: `injector.get(Foo) as MockFoo`. foo.setUpMock(); } ``` PR Close #13785
This commit is contained in:

committed by
Miško Hevery

parent
6d1f1a43bb
commit
d169c2434e
@ -6,6 +6,6 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {OpaqueToken} from '@angular/core';
|
||||
import {InjectionToken} from '@angular/core';
|
||||
|
||||
export const ON_WEB_WORKER = new OpaqueToken('WebWorker.onWebWorker');
|
||||
export const ON_WEB_WORKER = new InjectionToken('WebWorker.onWebWorker');
|
||||
|
@ -7,11 +7,12 @@
|
||||
*/
|
||||
|
||||
import {PlatformLocation} from '@angular/common';
|
||||
import {APP_INITIALIZER, NgZone} from '@angular/core';
|
||||
import {APP_INITIALIZER, InjectionToken, NgZone} from '@angular/core';
|
||||
|
||||
import {WebWorkerPlatformLocation} from './platform_location';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Those providers should be added when the router is used in a worker context in addition to the
|
||||
* {@link ROUTER_PROVIDERS} and after them.
|
||||
|
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ErrorHandler, Injectable, Injector, NgZone, OpaqueToken, PLATFORM_INITIALIZER, PlatformRef, Provider, RootRenderer, Testability, createPlatformFactory, isDevMode, platformCore} from '@angular/core';
|
||||
import {ErrorHandler, Injectable, InjectionToken, Injector, NgZone, PLATFORM_INITIALIZER, PlatformRef, Provider, RootRenderer, Testability, createPlatformFactory, isDevMode, platformCore} from '@angular/core';
|
||||
import {AnimationDriver, DOCUMENT, EVENT_MANAGER_PLUGINS, EventManager, HAMMER_GESTURE_CONFIG, HammerGestureConfig} from '@angular/platform-browser';
|
||||
|
||||
import {APP_ID_RANDOM_PROVIDER} from './private_import_core';
|
||||
@ -21,6 +21,7 @@ import {ServiceMessageBrokerFactory, ServiceMessageBrokerFactory_} from './web_w
|
||||
import {MessageBasedRenderer} from './web_workers/ui/renderer';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper class that exposes the Worker
|
||||
* and underlying {@link MessageBus} for lower level message passing.
|
||||
@ -42,7 +43,7 @@ export class WebWorkerInstance {
|
||||
/**
|
||||
* @experimental WebWorker support is currently experimental.
|
||||
*/
|
||||
export const WORKER_SCRIPT: OpaqueToken = new OpaqueToken('WebWorkerScript');
|
||||
export const WORKER_SCRIPT = new InjectionToken<string>('WebWorkerScript');
|
||||
|
||||
/**
|
||||
* A multi-provider used to automatically call the `start()` method after the service is
|
||||
@ -52,7 +53,7 @@ export const WORKER_SCRIPT: OpaqueToken = new OpaqueToken('WebWorkerScript');
|
||||
* @experimental WebWorker support is currently experimental.
|
||||
*/
|
||||
export const WORKER_UI_STARTABLE_MESSAGING_SERVICE =
|
||||
new OpaqueToken('WorkerRenderStartableMsgService');
|
||||
new InjectionToken<MessageBasedRenderer[]>('WorkerRenderStartableMsgService');
|
||||
|
||||
export const _WORKER_UI_PLATFORM_PROVIDERS: Provider[] = [
|
||||
{provide: NgZone, useFactory: createNgZone, deps: []},
|
||||
|
Reference in New Issue
Block a user