TSyringe is a lightweight dependency injection (DI) container for JavaScript and TypeScript, primarily focusing on constructor injection. Currently at stable version 4.10.0, it is actively maintained and backed by Microsoft. Its release cadence appears to be feature-driven, with recent updates (v4.4.0) adding interception and transformation capabilities. Key differentiators include its strong integration with TypeScript decorators (`@injectable`, `@singleton`, `@inject`), its ability to handle complex dependency graphs including circular dependencies with a `delay` helper, and its provision for both class-based and interface-based injection using tokens. It requires `reflect-metadata` for decorator-based type reflection and specific `tsconfig.json` settings.
npm install tsyringeVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic setup, interface-based dependency injection, and singleton registration.
Add or ensure the following in your `tsconfig.json`: `"compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true }`Install `reflect-metadata` (`npm install reflect-metadata`) and add `import "reflect-metadata";` as the very first line in your main application file (e.g., `main.ts` or `index.ts`).
Install `babel-plugin-transform-typescript-metadata` (`npm install --save-dev babel-plugin-transform-typescript-metadata`) and add it to your Babel configuration's `plugins` array.
Wrap the dependency registration or injection with `delay(() => DependencyClass)` or `delay(() => container.resolve(DependencyClass))`.
Ensure that all classes intended for injection are decorated with `@injectable()` and that any interfaces or custom tokens have a corresponding `container.register()` call.
Verify `reflect-metadata` is installed (`npm install reflect-metadata`) and that `import "reflect-metadata";` is the absolute first line in your application's entry point.
Check your `tsconfig.json` for `"experimentalDecorators": true` and `"emitDecoratorMetadata": true`. If using Babel, ensure `babel-plugin-transform-typescript-metadata` is installed and configured in your Babel plugins.