You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

tsyringe

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsyringe

Lightweight dependency injection container for JavaScript/TypeScript

4.10.0
latest
Source
npmnpm
Version published
Weekly downloads
617K
0.26%
Maintainers
2
Weekly downloads
 
Created

What is tsyringe?

tsyringe is a lightweight dependency injection container for TypeScript and JavaScript applications. It helps manage class dependencies and promotes the use of the Inversion of Control (IoC) principle, making it easier to write modular, testable, and maintainable code.

What are tsyringe's main functionalities?

Dependency Injection

This feature allows you to inject dependencies into classes. In the example, `Foo` is injected into `Bar`, and `Bar` can use `Foo`'s methods.

const { container, injectable, inject } = require('tsyringe');

@injectable()
class Foo {
  log() {
    console.log('Foo');
  }
}

@injectable()
class Bar {
  constructor(@inject(Foo) foo) {
    this.foo = foo;
  }
  log() {
    this.foo.log();
    console.log('Bar');
  }
}

const bar = container.resolve(Bar);
bar.log();

Singleton Registration

This feature allows you to register a class as a singleton, ensuring that only one instance of the class is created and shared across the application.

const { container, singleton } = require('tsyringe');

@singleton()
class SingletonService {
  constructor() {
    this.id = Math.random();
  }
}

const instance1 = container.resolve(SingletonService);
const instance2 = container.resolve(SingletonService);
console.log(instance1.id === instance2.id); // true

Scoped Registration

This feature allows you to register a class with a scoped lifecycle, meaning a new instance is created for each container scope.

const { container, scoped, Lifecycle } = require('tsyringe');

@scoped(Lifecycle.ContainerScoped)
class ScopedService {
  constructor() {
    this.id = Math.random();
  }
}

const childContainer = container.createChildContainer();
const instance1 = childContainer.resolve(ScopedService);
const instance2 = childContainer.resolve(ScopedService);
console.log(instance1.id === instance2.id); // true

const instance3 = container.resolve(ScopedService);
console.log(instance1.id === instance3.id); // false

Custom Providers

This feature allows you to register custom providers for dependencies, giving you more control over how dependencies are resolved.

const { container, inject, injectable } = require('tsyringe');

class CustomProvider {
  get() {
    return 'Custom Value';
  }
}

container.register('CustomProvider', { useClass: CustomProvider });

@injectable()
class Consumer {
  constructor(@inject('CustomProvider') provider) {
    this.provider = provider;
  }
  log() {
    console.log(this.provider.get());
  }
}

const consumer = container.resolve(Consumer);
consumer.log(); // Custom Value

Other packages similar to tsyringe

Keywords

dependency injection

FAQs

Package last updated on 16 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts