mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(core): Add Constructor type to injection types (#62265)
This allows us to pass in constructors into the inject function PR Close #62265
This commit is contained in:
parent
402eaa1c85
commit
ec01599987
4 changed files with 17 additions and 7 deletions
|
|
@ -4,11 +4,18 @@
|
|||
|
||||
```ts
|
||||
|
||||
// @public (undocumented)
|
||||
export function defineInjectable<T>(opts: {
|
||||
token: unknown;
|
||||
providedIn?: Type<any> | 'root' | 'platform' | 'any' | 'environment' | null;
|
||||
factory: () => T;
|
||||
}): unknown;
|
||||
|
||||
// @public (undocumented)
|
||||
export function getCurrentInjector(): Injector | undefined | null;
|
||||
|
||||
// @public (undocumented)
|
||||
export function inject<T>(token: InjectionToken<T>, options?: unknown): T | NotFound;
|
||||
export function inject<T>(token: InjectionToken<T> | Constructor<T>, options?: unknown): T | NotFound;
|
||||
|
||||
// @public
|
||||
export interface InjectionToken<T> {
|
||||
|
|
|
|||
|
|
@ -11,4 +11,4 @@ export type {Injector} from './src/injector';
|
|||
export {NOT_FOUND, NotFoundError, isNotFound} from './src/not_found';
|
||||
export type {NotFound} from './src/not_found';
|
||||
export type {InjectionToken, ɵɵInjectableDeclaration} from './src/injection_token';
|
||||
export {registerInjectable} from './src/injection_token';
|
||||
export {defineInjectable, registerInjectable} from './src/injection_token';
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ export function defineInjectable<T>(opts: {
|
|||
} as ɵɵInjectableDeclaration<T>;
|
||||
}
|
||||
|
||||
type Constructor<T> = Function & {prototype: T};
|
||||
export type Constructor<T> = Function & {prototype: T};
|
||||
|
||||
export function registerInjectable<T>(
|
||||
ctor: unknown,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
* found in the LICENSE file at https://angular.dev/license
|
||||
*/
|
||||
|
||||
import {InjectionToken} from './injection_token';
|
||||
import {Constructor, InjectionToken} from './injection_token';
|
||||
import {NotFound, NOT_FOUND} from './not_found';
|
||||
|
||||
export interface Injector {
|
||||
|
|
@ -33,10 +33,13 @@ export function setCurrentInjector(
|
|||
return former;
|
||||
}
|
||||
|
||||
export function inject<T>(token: InjectionToken<T>, options?: unknown): T | NotFound {
|
||||
export function inject<T>(
|
||||
token: InjectionToken<T> | Constructor<T>,
|
||||
options?: unknown,
|
||||
): T | NotFound {
|
||||
const currentInjector = getCurrentInjector();
|
||||
if (!currentInjector) {
|
||||
if (!currentInjector || !(token as InjectionToken<T>).ɵprov) {
|
||||
return NOT_FOUND;
|
||||
}
|
||||
return currentInjector.retrieve(token, options);
|
||||
return currentInjector.retrieve(token as InjectionToken<T>, options);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue