# Injectable Decorator

Module
import { Injectable } from "@tsed/di"
Source/packages/di/src/decorators/injectable.ts

# Overview

function Injectable(options?: Partial<IProvider<any>>): Function;

# Description

The decorators @Injectable() a new service can be injected in other service, controller, interceptor, etc.. on there constructor. All classes annotated with @Injectable() are built one time, excepted if you change the default provider configuration.

import {Injectable, ProviderScope, ProviderType} from "@tsed/common";
import {Calendar} from "../models/Calendar";

@Injectable({
  type: ProviderType.CONTROLLER,
  scope: ProviderScope.SINGLETON
})
export class CalendarsService {
  private readonly calendars: Calendar[] = [];

  create(calendar: Calendar) {
    this.calendars.push(calendar);
  }

  findAll(): Calendar[] {
    return this.calendars;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

TIP

@Injectable() use the reflect-metadata to collect and inject the built provided to other services.

# Options

  • type ( or string): Kind of provider. (Default: ProviderType.PROVIDER)
  • scope (@@ProviderScope@): Kind of provider. (Default: ProviderScope.SINGLETON)
  • deps (Type<any>): List of class or provider which will be injected to the constructor (Note: This options override default metadata generated by Typescript).