{"id":12218,"library":"typed-inject","title":"Typed Inject","description":"Typed Inject is a robust, 100% type-safe dependency injection framework specifically designed for TypeScript applications. It allows developers to inject classes, interfaces, or primitive values, leveraging TypeScript's type system to ensure that if a project compiles, its dependencies are correctly resolved at runtime with their declared types. The current stable version is 5.0.0, released in late 2024. Major versions typically roll out with significant changes like Node.js version updates or module system migrations, as seen with the v4.0.0 (ESM migration) and v5.0.0 (Node 16 drop) releases. Its primary differentiator is its strong type-safety, which virtually eliminates runtime dependency resolution errors by shifting validation to compile time. It handles various injection patterns, including class, value, and factory providers, and supports concepts like child injectors and lifecycle control.","status":"active","version":"5.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/nicojs/typed-inject","tags":["javascript","typescript","dependency-injection","IoC","di","inject","type-safe"],"install":[{"cmd":"npm install typed-inject","lang":"bash","label":"npm"},{"cmd":"yarn add typed-inject","lang":"bash","label":"yarn"},{"cmd":"pnpm add typed-inject","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"typed-inject migrated to native ESM in v4.0.0. CommonJS `require` statements will fail.","wrong":"const { createInjector } = require('typed-inject');","symbol":"createInjector","correct":"import { createInjector } from 'typed-inject';"},{"note":"Use `import type` for type-only imports to avoid bundling issues and clearly signal intent.","wrong":"import { Injector } from 'typed-inject';","symbol":"Injector","correct":"import type { Injector } from 'typed-inject';"},{"note":"While less common, `InjectionToken` is a key type for advanced usage and can be imported as a type.","symbol":"InjectionToken","correct":"import type { InjectionToken } from 'typed-inject';"}],"quickstart":{"code":"import { createInjector } from 'typed-inject';\n\ninterface Logger {\n  info(message: string): void;\n}\n\nconst logger: Logger = {\n  info(message: string) {\n    console.log(`[Logger]: ${message}`);\n  },\n};\n\nclass HttpClient {\n  constructor(private log: Logger) {}\n  public static inject = ['logger'] as const;\n\n  fetchData(url: string) {\n    this.log.info(`Fetching data from ${url}`);\n    return Promise.resolve(`Data from ${url}`);\n  }\n}\n\nclass MyService {\n  constructor(\n    private http: HttpClient,\n    private log: Logger,\n  ) {}\n  public static inject = ['httpClient', 'logger'] as const;\n\n  async doSomething() {\n    this.log.info('MyService doing something...');\n    const data = await this.http.fetchData('https://example.com/api/data');\n    this.log.info(`Received: ${data}`);\n    return data;\n  }\n}\n\nconst appInjector = createInjector()\n  .provideValue('logger', logger)\n  .provideClass('httpClient', HttpClient);\n\n// Create an instance of MyService with all dependencies resolved\nconst myService = appInjector.injectClass(MyService);\n\nmyService.doSomething();\n// This demonstrates setting up an injector, providing a value and a class, and then injecting a dependent class.","lang":"typescript","description":"This quickstart demonstrates how to set up `typed-inject` by creating an injector, providing a value and a class, and then injecting a service that depends on both."},"warnings":[{"fix":"Upgrade your Node.js runtime to version 18 or later. Check `engines` field in package.json.","message":"Version 5.0.0 of typed-inject dropped official support for Node.js 16. Projects must use Node.js 18 or newer.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Migrate your project to use ES Modules with `import` statements. Ensure your `package.json` specifies `\"type\": \"module\"` or uses `.mjs` extensions for modules.","message":"Version 4.0.0 of typed-inject migrated to native ESM. CommonJS `require` statements will no longer work and may lead to runtime errors.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Upgrade your Node.js runtime to version 16 or later.","message":"Version 4.0.0 of typed-inject dropped official support for Node.js 14. Projects must use Node.js 16 or newer.","severity":"breaking","affected_versions":">=4.0.0 <5.0.0"},{"fix":"Ensure you are using TypeScript 3.0+ (preferably 4.5+ to avoid specific compiler bugs) and enable `--strict` or at least `--strictFunctionTypes` in your `tsconfig.json`.","message":"typed-inject requires TypeScript version 3.0 or above. Additionally, a known bug in TypeScript versions >3.8 and <4.5 might prevent some type errors from being caught.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Add `\"strictFunctionTypes\": true` or `\"strict\": true` to your `compilerOptions` in `tsconfig.json`.","message":"To ensure full type-safety and proper error catching, `--strictFunctionTypes` (or `--strict`) must be enabled in your TypeScript configuration.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const { createInjector } = require('typed-inject');` to `import { createInjector } from 'typed-inject';` and ensure your project is configured for ESM.","cause":"Attempting to use CommonJS `require()` syntax after typed-inject migrated to native ES Modules in v4.0.0.","error":"TypeError: createInjector is not a function"},{"fix":"Verify that `appInjector.provideValue`, `provideClass`, or `provideFactory` methods are called for all required dependencies (e.g., `appInjector.provideValue('someDependency', instance);`) and that the token names match exactly.","cause":"A required dependency was not provided to the injector, or the injection token name does not match the provided name.","error":"Error: Could not resolve dependency for token \"someDependency\""},{"fix":"Ensure the class has a `public static inject = ['dependencyName1', 'dependencyName2'] as const;` property, and that the names listed correspond to correctly provided dependencies and match the constructor's parameters in order and type.","cause":"Likely due to missing or incorrect `static inject` property on the class being injected, or a mismatch between the provided dependency types and the constructor's expected types.","error":"TS2345: Argument of type 'typeof MyClass' is not assignable to parameter of type 'new (...args: any[]) => MyClass'."}],"ecosystem":"npm"}