injection-js
Version:
Dependency Injection library for JavaScript and TypeScript
49 lines (48 loc) • 2.09 kB
TypeScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { Injector } from './injector';
import { ProviderToken } from './provider-token';
export declare function getCurrentInjector(): Injector | undefined;
export declare function setCurrentInjector(injector: Injector | undefined): Injector | undefined;
export interface InjectOptions {
optional?: boolean;
}
export declare function inject<T>(token: ProviderToken<T>, options?: InjectOptions & {
optional?: false;
}): T;
export declare function inject<T = any>(token: ProviderToken, options?: InjectOptions & {
optional?: false;
}): T;
export declare function inject<T>(token: ProviderToken<T>, options?: InjectOptions & {
optional?: true;
}): T | null;
export declare function inject<T = any>(token: ProviderToken, options?: InjectOptions & {
optional?: true;
}): T | null;
/**
* Runs the given function in the context of the given {@link Injector}.
*
* Within the function's stack frame, {@link inject} can be used to inject dependencies
* from the given {@link Injector}. Note that {@link inject} is only usable synchronously, and cannot be used in
* any asynchronous callbacks or after any `await` points.
*
* @param injector the injector which will satisfy calls to {@link inject} while `fn` is executing
* @param fn the closure to be run in the context of {@link injector}
* @returns the return value of the function, if any
*/
export declare function runInInjectionContext<ReturnT>(injector: Injector, fn: () => ReturnT): ReturnT;
/**
* Whether the current stack frame is inside an injection context.
*/
export declare function isInInjectionContext(): boolean;
/**
* Asserts that the current stack frame is within an injection context and has access to `inject`.
*
* @param debugFn a reference to the function making the assertion (used for the error message).
*/
export declare function assertInInjectionContext(debugFn: Function): void;