{"name":"TSyringe: Lightweight Dependency Injection for TypeScript/JavaScript","description":"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","url":"https://osrepos.com/repo/microsoft-tsyringe","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/microsoft-tsyringe","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/microsoft-tsyringe.md","json":"https://osrepos.com/repo/microsoft-tsyringe.json","topics":["container","decorators","dependency-injection","ioc","typescript","javascript","development"],"keywords":["container","decorators","dependency-injection","ioc","typescript","javascript","development"],"stars":null,"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.","content":"## Introduction\nTSyringe 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.\n\n## Installation\nTo get started with TSyringe, you need to install it via npm or yarn and configure your TypeScript project.\n\nFirst, install the package:\n\nsh\nnpm install --save tsyringe\n# or\nyarn add tsyringe\n\n\nNext, modify your `tsconfig.json` to enable experimental decorators and emit decorator metadata:\n\n\n{\n  \"compilerOptions\": {\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true\n  }\n}\n\n\nFinally, add a polyfill for the Reflect API, typically `reflect-metadata`, at the entry point of your application:\n\ntypescript\n// main.ts\nimport \"reflect-metadata\";\n\n// Your application code here...\n\n\n## Examples\nTSyringe makes it straightforward to inject dependencies, especially when working with interfaces to promote loose coupling. Here's an example demonstrating dependency injection with interfaces:\n\ntypescript\n// SuperService.ts\nexport interface SuperService {\n  doSomething(): string;\n}\n\n// TestService.ts\nimport { SuperService } from \"./SuperService\";\n\nexport class TestService implements SuperService {\n  doSomething(): string {\n    return \"Hello from TestService!\";\n  }\n}\n\n// Client.ts\nimport { injectable, inject } from \"tsyringe\";\nimport { SuperService } from \"./SuperService\";\n\n@injectable()\nexport class Client {\n  constructor(@inject(\"SuperService\") private service: SuperService) {}\n\n  execute(): string {\n    return `Client executing: ${this.service.doSomething()}`;\n  }\n}\n\n// main.ts\nimport \"reflect-metadata\";\nimport { container } from \"tsyringe\";\nimport { Client } from \"./Client\";\nimport { TestService } from \"./TestService\";\n\n// Register the implementation for the 'SuperService' token\ncontainer.register(\"SuperService\", {\n  useClass: TestService\n});\n\nconst client = container.resolve(Client);\nconsole.log(client.execute()); // Output: Client executing: Hello from TestService!\n\n\n## Why Use TSyringe?\nAdopting a dependency injection container like TSyringe offers several significant advantages for your projects:\n\n*   **Improved Testability:** Dependencies can be easily mocked or swapped out during unit testing, simplifying the testing process.\n*   **Loose Coupling:** Components are less dependent on concrete implementations, making your codebase more flexible and easier to refactor.\n*   **Enhanced Maintainability:** Centralized management of dependencies reduces boilerplate code and makes it easier to understand and modify how components interact.\n*   **Scalability:** Supports the development of larger, more complex applications by providing a structured way to manage dependencies across many modules.\n*   **Clearer Code Structure:** Promotes a clean architecture and adherence to SOLID principles, leading to more organized and readable code.\n\n## Links\nFor more detailed information, documentation, and to contribute, visit the official TSyringe resources:\n\n*   [GitHub Repository](https://github.com/microsoft/tsyringe)\n*   [npm Package](https://www.npmjs.com/package/tsyringe)","metrics":{"detailViews":12,"githubClicks":9},"dates":{"published":null,"modified":"2026-02-12T12:01:22.000Z"}}