OptionalLazyInject
@tsed/di
Usage
typescript
import { OptionalLazyInject } from "@tsed/di";Overview
ts
function OptionalLazyInject(resolver: () => Promise<{
default: unknown;
}>): PropertyDecorator;
export function OptionalLazyInject(key: string, resolver: () => Promise<{
default: unknown;
}>): PropertyDecorator;Description
Optionally inject a provider from a dynamic import, returning undefined on failure.
Similar to @LazyInject but gracefully handles import failures by returning undefined. Useful for truly optional dependencies that may not be available in all environments.
Usage
typescript
import type {PlatformException} from "@tsed/platform-exceptions";
import {Injectable, OptionalLazyInject} from "@tsed/di";
@Injectable()
export class MyService {
@OptionalLazyInject(() => import("@tsed/platform-exceptions"))
exceptions?: Promise<PlatformException>;
async handleError(error: Error) {
const handler = await this.exceptions;
if (handler) {
handler.handle(error);
} else {
console.error("Exception handler not available:", error);
}
}
}default
ts
default: unknown;default
ts
default: unknown;