Get unlimited public & private packages + package-based permissions with npm Pro.Get started »


1.0.1 • Public • Published


A seamless dependency injector for typescript and javascript.


npm install tsjs-di



Importing the dependency injector

import {DI} from 'tsjs-di';

Creating providers


export class ProviderA {
    name = 'ProviderA';

Providers will be injected as singletons into all dependants, to inject multi instance a provider must be decorated with @Provider({multi: true}) or registered with the multi option set to true.

A Provider can also have its dependencies injected if it is decorated with @Provider() or Inject().


import {Provider} from 'tsjs-di';
import {ProviderA} from './providerA';
@Provider({multi: true})
export class ProviderB {
    name = 'providerB';
    constructor(private provider: ProviderA){

Creating dependants

A dependant class must be marked with @Inject() decorator to have its dependencies injected.


import {Inject} from 'tsjs-di';
import {ProviderA} from './providerA';
export class DependantA {
    provider: ProviderA;
    // the type of the provider is important to have the dependency inject by the DI
    constructor(provider: ProviderA){
        this.provider = provider;

A dependency can also be injected into a class property by decorating the property with @Inject() decorator and specifying the type of the dependency.


import {Inject} from 'tsjs-di';
import {ProviderB} from './providerB';
export class DependantB{
    provider: ProviderB;

Register providers and running the DI


import {DI} from 'tsjs-id';
import {ProviderA} from './providerA';
import {ProviderB} from './providerB';
import {DependantA} from './dependantA';
import {DependantB} from './dependantB';
// register by specifying the class only
// Or by supplying a provider object
DI.register({provide: ProviderB, useClass: ProviderB});
// specifying a factory
DI.register({provide: ProvideA, useFactory: (di) => {
    return new ProviderA();
// Or by specifiying an array with the above methods
    {provide: ProviderB, useClass: ProviderB}
// You can also provide a value;
export class Config {
    name: string,
    author: string;
const value: Config = {
    name: 'tsjs-di',
    author: 'Jonas Tomanga';
DI.register({provide: 'Config', useValue: value });
// putting the DI into action
class App{
    provider: ProviderB;
    constructor(private providerA: ProviderA){
        return {
const app = new App(); // this will produce an error in the IDE
// altentively
const app = new App(undefined);

The DI will inject the dependencies as long as the parameters are not defined on call.


Since there is no type support in javascript, dependencies have to be injected manually. This methods can also be used in typescript.

Simple Example

const DI = require('tsjs-di').DI;
class Provider {
    name = 'Provider';
class Dependant {
    constructor(provider = DI.inject('Provider')){
        this.provider = provider;
// register the provider
const dep = new Dependant();
// or
const dep = new Depedant(DI.inject(Provider));

Factory Example


class Logger {
module.exports = Logger;


const DI = require('tsjs-di').DI;
class FactoryProvider {
        this.logger = logger;
    static create(di){
        return new FactoryProvider(di.inject('Logger'));
module.exports = FactoryProvider;


const DI = require('tsjs-di').DI;
const Logger = require('logger');
const FactoryProvider = require('factory-provider');
    {provide: FactoryProvider, useFactory: FactoryProvider.create}
const provider = DI.inject('FactoryProvider');
provide.log('Hello, world!');



Adds dependency providers to the DI, it takes a class, a provider object or an array of classes and provider object.

provider object

The provider object must have:

  • provide: A string or a Class of the provider

and any of the following:

  • useClass: The class to be used to satisfy the dependency, can be different from the one provided.
  • useFactory: A function to be used to create or instantiate the provider.
  • useValue: The value to be used to satisfy the dependency, it can be of any type.
  • multi: A boolean that indicates if the provider should be multi instance or not.

Throws a TypeError if called with an invalid provider


Returns a registered provider that matches the specified type. Returns the same instance unless the dependency was registered with the multi option. type can be a string or Class from which the provider can be identified.

Throws a ReferenceError if the provider is not registered.


Returns a new instance of a registered provider that matches the specified type. type can be a string or Class from which the provider can be identified.

Throws a ReferenceError if the provider is not registered.


Returns true if the specified provider exists, else returns false. type can be a string or Class from which the provider can be identified.


Removes all providers


npm i tsjs-di

DownloadsWeekly Downloads






Last publish


  • avatar