Constant
@tsed/di
Usage
typescript
import { Constant } from "@tsed/di";Overview
ts
function Constant<Type = unknown>(expression: string, defaultValue?: Type): PropertyDecorator;expression (
string): Dot-notation path to the configuration valuedefaultValue (
Type): Optional. default value if not found
Description
Inject a frozen configuration value into a property.
Retrieves a value from the injector configuration and freezes it (creates an immutable deep clone). The value is lazily loaded on first access and cached for subsequent accesses.
Usage
typescript
import {Injectable, Constant} from "@tsed/di";
import {Env} from "@tsed/core";
@Injectable()
export class MyService {
@Constant("env")
env: Env;
@Constant("api.baseUrl")
apiUrl: string;
@Constant("api.timeout", 5000)
timeout: number;
constructor() {
// Not available yet - undefined
}
$onInit() {
console.log(this.env); // Value is available
}
}