Kind: Type
Source: packages/common/decorators/core/injectable.decorator.ts
Part of: Common
Defines the injection scope.
InjectableOptions configures how a provider is registered when using NestJS's @Injectable() decorator. Its primary responsibility is defining the provider's injection scope, which controls whether Nest creates a shared singleton, a per-request instance, or a transient instance.
Definition
tsScopeOptions
Diagram
mermaidgraph LR A["@Injectable(options)"] --> B["InjectableOptions"] B --> C["scope?: Scope"] C --> D["DEFAULT<br/>Shared singleton"] C --> E["REQUEST<br/>One instance per request"] C --> F["TRANSIENT<br/>New instance per injection"] A --> G["Nest DI Container"]
Usage
tsimport { Injectable, Scope } from '@nestjs/common';
@Injectable({
scope: Scope.REQUEST,
})
export class RequestContextService {
private readonly createdAt = new Date();
getCreatedAt(): Date {
return this.createdAt;
}
}
AI Coding Instructions
- Use
InjectableOptionsas the options object passed to@Injectable(). - Prefer the default scope for stateless services and shared infrastructure such as repositories or API clients.
- Use
Scope.REQUESTonly when a provider needs request-specific state, such as tenant or correlation context. - Use
Scope.TRANSIENTfor providers that must be recreated for every injection site. - Be aware that request-scoped or transient dependencies can cause otherwise singleton consumers to become scoped as well.
Was this page helpful?