# TSyringe: Lightweight Dependency Injection for TypeScript/JavaScript

This repository profile is provided by osrepos.com, an open source repository discovery platform.

Source: osrepos.com
Repository profile: https://osrepos.com/repo/microsoft-tsyringe
Generated for open source discovery and AI-assisted research.

TSyringe is a lightweight dependency injection (DI) container developed by Microsoft for JavaScript and TypeScript applications. It provides a robust solution for managing class dependencies, making your codebase more modular, testable, and maintainable. By leveraging decorators, TSyringe simplifies the process of injecting dependencies into your classes, adhering to the Inversion of Control (IoC) principle.

GitHub: https://github.com/microsoft/tsyringe
OSRepos URL: https://osrepos.com/repo/microsoft-tsyringe

## Summary

TSyringe is a lightweight dependency injection (DI) container developed by Microsoft for JavaScript and TypeScript applications. It provides a robust solution for managing class dependencies, making your codebase more modular, testable, and maintainable. By leveraging decorators, TSyringe simplifies the process of injecting dependencies into your classes, adhering to the Inversion of Control (IoC) principle.

## Topics

- container
- decorators
- dependency-injection
- ioc
- typescript
- javascript
- development

## Repository Information

Last analyzed by OSRepos: Thu Feb 12 2026 12:01:22 GMT+0000 (Western European Standard Time)
Detail views: 12
GitHub clicks: 9

## Safety Notice

OSRepos shares public repositories for knowledge and discovery only. Review source code, dependencies, licenses, and security implications before running or installing anything.

## Content

## Introduction
TSyringe is a lightweight dependency injection (DI) container developed by Microsoft for JavaScript and TypeScript applications. It provides a robust solution for managing class dependencies, making your codebase more modular, testable, and maintainable. By leveraging decorators, TSyringe simplifies the process of injecting dependencies into your classes, adhering to the Inversion of Control (IoC) principle.

## Installation
To get started with TSyringe, you need to install it via npm or yarn and configure your TypeScript project.

First, install the package:

sh
npm install --save tsyringe
# or
yarn add tsyringe


Next, modify your `tsconfig.json` to enable experimental decorators and emit decorator metadata:


{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}


Finally, add a polyfill for the Reflect API, typically `reflect-metadata`, at the entry point of your application:

typescript
// main.ts
import "reflect-metadata";

// Your application code here...


## Examples
TSyringe makes it straightforward to inject dependencies, especially when working with interfaces to promote loose coupling. Here's an example demonstrating dependency injection with interfaces:

typescript
// SuperService.ts
export interface SuperService {
  doSomething(): string;
}

// TestService.ts
import { SuperService } from "./SuperService";

export class TestService implements SuperService {
  doSomething(): string {
    return "Hello from TestService!";
  }
}

// Client.ts
import { injectable, inject } from "tsyringe";
import { SuperService } from "./SuperService";

@injectable()
export class Client {
  constructor(@inject("SuperService") private service: SuperService) {}

  execute(): string {
    return `Client executing: ${this.service.doSomething()}`;
  }
}

// main.ts
import "reflect-metadata";
import { container } from "tsyringe";
import { Client } from "./Client";
import { TestService } from "./TestService";

// Register the implementation for the 'SuperService' token
container.register("SuperService", {
  useClass: TestService
});

const client = container.resolve(Client);
console.log(client.execute()); // Output: Client executing: Hello from TestService!


## Why Use TSyringe?
Adopting a dependency injection container like TSyringe offers several significant advantages for your projects:

*   **Improved Testability:** Dependencies can be easily mocked or swapped out during unit testing, simplifying the testing process.
*   **Loose Coupling:** Components are less dependent on concrete implementations, making your codebase more flexible and easier to refactor.
*   **Enhanced Maintainability:** Centralized management of dependencies reduces boilerplate code and makes it easier to understand and modify how components interact.
*   **Scalability:** Supports the development of larger, more complex applications by providing a structured way to manage dependencies across many modules.
*   **Clearer Code Structure:** Promotes a clean architecture and adherence to SOLID principles, leading to more organized and readable code.

## Links
For more detailed information, documentation, and to contribute, visit the official TSyringe resources:

*   [GitHub Repository](https://github.com/microsoft/tsyringe)
*   [npm Package](https://www.npmjs.com/package/tsyringe)