TypeDI is a dependency injection framework specifically designed for TypeScript and JavaScript applications. It enables the creation of loosely coupled, well-structured, and easily testable applications in both Node.js and browser environments. The current stable version is 0.10.0, with releases occurring periodically to introduce new features, improvements, and bug fixes, as indicated by the recent 0.9.x to 0.10.0 progression. Key differentiators include support for both property and constructor-based injection, management of singleton and transient services, and the ability to work with multiple DI containers within a single application. It heavily leverages TypeScript decorators and the `reflect-metadata` polyfill to achieve its injection capabilities, making proper configuration of `tsconfig.json` crucial for its operation.
npm install typediVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic setup with `reflect-metadata` and TypeDI's `@Service()` decorator for both constructor and property injection, including usage of environment variables.
Upgrade to the latest stable version and review the official documentation for API changes, especially around `@Service` and `Container` methods.
Add `import 'reflect-metadata';` to the absolute top of your main application file (e.g., `src/main.ts` or `app.ts`).
Ensure the specified `compilerOptions` are present in your `tsconfig.json` file. Without them, decorators will not function as expected for DI.
Refactor your services to break circular dependencies, perhaps by introducing an interface, an event emitter, or restructuring responsibilities. Lazy injection patterns might also help in specific scenarios.
Ensure `import 'reflect-metadata';` is the absolute first line in your main application file before any other imports.
Verify that `MyService` has `@Service()` applied. Check `tsconfig.json` for `emitDecoratorMetadata` and `experimentalDecorators`. Inspect dependency graph for circular references.
Add `"reflect-metadata"` to the `types` array in your `tsconfig.json`'s `compilerOptions`, or ensure `"lib": ["esnext", "dom"]` (or similar) is present which often includes `esnext.reflect`.