Skip to content

AutoInjectable

@tsed/di

Usage

typescript
import { AutoInjectable } from "@tsed/di";

See /packages/di/src/types/common/decorators/autoInjectable.ts.

Overview

ts
function AutoInjectable(): <T extends new (...args: any[]) => NonNullable<unknown>>(constr: T) => T;

Description

AutoInjectable decorator is used to automatically inject dependencies in the constructor.

typescript
@AutoInjectable()
class UserService {
  constructor(
    private database: Database,  // automatically injected
    customConfig?: Config        // can be passed manually
  ) {}
}

When applied to a class, this decorator:

  • Automatically resolves and injects dependencies based on constructor parameter types
  • Preserves the ability to manually pass arguments which override automatic injection

Released under the MIT License.