1. typedi
Dependency injection for TypeScript.
typedi
Package: typedi
Created by: pleerock
Last modified: Fri, 09 Dec 2022 18:09:28 GMT
Version: 0.10.0
License: MIT
Downloads: 948,543
Repository: https://github.com/pleerock/typedi

Install

npm install typedi
yarn add typedi

TypeDI

Build Status
codecov
npm version
Dependency Status

TypeDI is a dependency injection tool for TypeScript and JavaScript. With it you can build well-structured and easily testable applications in Node or in the browser.

Main features includes:

  • property based injection
  • constructor based injection
  • singleton and transient services
  • support for multiple DI containers

Installation

Note: This installation guide is for usage with TypeScript, if you wish to use
TypeDI without Typescript please read the documentation about how get started.

To start using TypeDI install the required packages via NPM:

 npm install typedi reflect-metadata

Import the reflect-metadata package at the first line of your application:

 import 'reflect-metadata';

// Your other imports and initialization code
// comes here after you imported the reflect-metadata package!

As a last step, you need to enable emitting decorator metadata in your Typescript config. Add these two lines to your tsconfig.json file under the compilerOptions key:

 "emitDecoratorMetadata": true,
"experimentalDecorators": true,

Now you are ready to use TypeDI with Typescript!

Basic Usage

 import { Container, Service } from 'typedi';

@Service()
class ExampleInjectedService {
  printMessage() {
    console.log('I am alive!');
  }
}

@Service()
class ExampleService {
  constructor(
    // because we annotated ExampleInjectedService with the @Service()
    // decorator TypeDI will automatically inject an instance of
    // ExampleInjectedService here when the ExampleService class is requested
    // from TypeDI.
    private injectedService: ExampleInjectedService
  ) {}
}

const serviceInstance = Container.get(ExampleService);
// we request an instance of ExampleService from TypeDI

serviceInstance.injectedService.printMessage();
// logs "I am alive!" to the console

Documentation

The detailed usage guide and API documentation for the project can be found:

Contributing

Please read our contributing guidelines to get started.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code