Skip to content

ValueProvider

reference
1 min readUpdated

Kind: Interface

Source: packages/common/interfaces/modules/provider.interface.ts

Part of: Common

Interface defining a Value type provider.

For example:

typescript
const connectionProvider = {
  provide: 'CONNECTION',
  useValue: connection,
};

ValueProvider defines a dependency injection provider that registers an existing value under an InjectionToken. Use it when a dependency is already created—such as a configuration object, database connection, or third-party client—and should be shared through the container without factory logic.

Properties

PropertyType
provideInjectionToken
useValueT
injectnever

Diagram

mermaid
graph LR
  A[Existing value] --> B[ValueProvider]
  B -->|provide: InjectionToken| C[Dependency Injection Container]
  C -->|resolve token| D[Consumer]
  B -->|useValue: T| A

Usage

typescript
import type { ValueProvider } from '@nestjs/common';

const connection = createDatabaseConnection();

const connectionProvider: ValueProvider = {
  provide: 'CONNECTION',
  useValue: connection,
};

// Consumers can inject the registered value using the same token.
class UserRepository {
  constructor(
    @Inject('CONNECTION')
    private readonly connection: DatabaseConnection,
  ) {}
}

AI Coding Instructions

  • Use useValue for pre-existing objects, constants, configuration, mocks, or initialized third-party clients.
  • Ensure the provide token exactly matches the token used by consuming classes or factories.
  • Do not add inject dependencies to a value provider; useValue is supplied directly and inject is never.
  • Prefer symbols or exported token constants over repeated string literals for shared application dependencies.
  • Use ValueProvider in tests to replace real dependencies with deterministic mock objects.

Used by

4 references from 4 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (4)

  • isClassProviderpackages/core/injector/helpers/provider-classifier.ts:9
  • InternalCoreModulepackages/core/injector/internal-core-module/internal-core-module.ts:16
  • Modulepackages/core/injector/module.ts:44
  • DependenciesScannerpackages/core/scanner.ts:75

Was this page helpful?

Download as PDF
ValueProvider — NestJS head-to-head