Registry / web-framework / typescript-ioc

typescript-ioc

JSON →
library3.2.2jsnpmunverified

typescript-ioc is a lightweight, annotation-based dependency injection (DI) container specifically designed for TypeScript applications. It allows developers to manage dependencies using decorators like `@Inject`, simplifying object graph creation and promoting modular design. The library is currently stable at version 3.2.2, with the 3.x series being actively maintained through frequent patch and minor releases. Key differentiators include its small footprint, ease of use with TypeScript decorators, and broad environmental support including Node.js, browsers, and React Native. It provides features such as different scopes (Singleton, Request, Local), factory creation, and configuration capabilities, making it suitable for both simple and complex application architectures where inversion of control is desired. A notable change in the 3.x series was the discontinuation of support for ES5 runtimes, requiring modern JavaScript environments (ES6 or newer).

npm install typescript-ioc
INSTALL
IMPORT
SIG · TYPESCRIPT-IOC
T
typescript-ioc
web-frameworkjavascriptv3.2.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Inject
import { Inject } from 'typescript-ioc';
const Inject = require('typescript-ioc').Inject;
Used as a decorator for properties or constructor parameters to mark them for dependency injection. Requires TypeScript decorator metadata.
Container
import { Container } from 'typescript-ioc';
import * as IoC from 'typescript-ioc'; const Container = IoC.Container;
The central class for manual dependency resolution, configuration, and testing. Provides methods like `get()` and `bind()`.
Scope
import { Scope } from 'typescript-ioc';
import { ScopeType } from 'typescript-ioc';
Enum used with the `@Scope` decorator to define the lifecycle of injected instances (e.g., `Scope.Singleton`, `Scope.Request`).

Demonstrates basic setup with `@Inject` for property and named value injection, `@Scope` for singleton management, and `Container.bind()` for interface-to-implementation mapping. Also shows manual resolution and direct instantiation.

import { Inject, Container, Scope } from "typescript-ioc"; // Ensure your tsconfig.json has: // "experimentalDecorators": true, // "emitDecoratorMetadata": true, // "target": "es6" interface ILogger { log(message: string): void; } @Scope(Scope.Singleton) class ConsoleLogger implements ILogger { private readonly timestamp: Date; constructor() { this.timestamp = new Date(); console.log(`ConsoleLogger instance created at ${this.timestamp.toISOString()}`); } log(message: string): void { console.log(`[${this.timestamp.toISOString()}] ${message}`); } } class DatabaseService { @Inject private logger!: ILogger; // '!' is TypeScript's definite assignment assertion connect(): void { this.logger.log("Attempting to connect to database..."); // Simulate database connection logic } } class UserService { @Inject private dbService!: DatabaseService; @Inject('APP_NAME') // Injecting a constant value defined by Container.bindName private appName!: string; createUser(username: string): void { this.dbService.connect(); console.log(`[${this.appName}] Creating user: ${username}`); // Simulate user creation logic } } // Configure the container: bind an interface to an implementation Container.bind(ILogger).to(ConsoleLogger); // Bind a named constant value Container.bindName('APP_NAME').to('MyAwesomeApp'); // Option 1: Get instance from the container to ensure all dependencies are resolved const userServiceFromContainer = Container.get(UserService); userServiceFromContainer.createUser("Alice"); // Option 2: Instantiate directly, and the container will inject dependencies const anotherUserService = new UserService(); anotherUserService.createUser("Bob"); // Verify singleton scope for logger (should show the same creation timestamp) const logger1 = Container.get(ILogger); const logger1Again = Container.get(ILogger); console.log('Are logger1 and logger1Again the same instance?', logger1 === logger1Again);
Debug
Known issues
breakingVersion 3.0.0 and above of `typescript-ioc` removed support for ES5 runtimes. Projects must target ES6 or newer.
fix
Ensure your `tsconfig.json` includes `'target': 'es6'` or a newer ECMAScript version in `compilerOptions`.
affects: >=3.0.0
gotchaTypeScript decorators are a required feature and need specific compiler options enabled to function correctly with `typescript-ioc`.
fix
Add `'experimentalDecorators': true` and `'emitDecoratorMetadata': true` to `compilerOptions` in your `tsconfig.json`.
affects: >=1.0.0
gotchaWhen injecting an interface, `typescript-ioc` requires an explicit binding to a concrete implementation via `Container.bind(Interface).to(Implementation)`. It cannot infer the implementation from the interface type alone.
fix
For an interface `ILogger` and its implementation `ConsoleLogger`, add `Container.bind(ILogger).to(ConsoleLogger);` before resolving `ILogger`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'constructor')
`emitDecoratorMetadata` is not enabled in `tsconfig.json`, preventing TypeScript from emitting necessary type information for `typescript-ioc` to resolve dependencies.
fix
Add `'emitDecoratorMetadata': true` to `compilerOptions` in your `tsconfig.json`.
Error: Decorators are not enabled.
`experimentalDecorators` is not set to `true` in your `tsconfig.json`, which is required for all TypeScript decorators.
fix
Add `'experimentalDecorators': true` to `compilerOptions` in your `tsconfig.json`.
Error: Class or instance not registered in container
Attempted to `Container.get(MyInterface)` without a prior `Container.bind(MyInterface).to(MyImplementation)` or a class that is not decorated/registered correctly.
fix
For interfaces, ensure an explicit `Container.bind(Interface).to(Implementation);` exists. For classes, ensure they are `@Inject`-able or bound.
Upgrade
Version history
3.2.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
OpenAI (training)
1
Resources
typescript-ioc — npm install typescript-ioc · libregistry