Skip to main content

injectOptional()

function injectOptional\<T\>(token: InjectionToken\<T\>): T | undefined;

Defined in: src/lib/di/index.ts:178

Try to inject a service, returning undefined if not found.

Useful for optional dependencies that may not be available.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
tokenInjectionToken<T>The service class or injection token to inject.

Returns

T | undefined

The service instance, or undefined if not found.

Example

import { injectOptional } from "@core/di";
import { DebugService } from "@modules/debug/debug.service";

class MyService {
// DebugService might not be loaded in production
private debugService = injectOptional(DebugService);

log(message: string) {
this.debugService?.log(message);
}
}