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
muslnode 18–226 runs
build_error
glibcnode 18–226 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);
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.
fixAdd `'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.
fixAdd `'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.
fixFor interfaces, ensure an explicit `Container.bind(Interface).to(Implementation);` exists. For classes, ensure they are `@Inject`-able or bound.
Audit
Dependencies
No dependency data recorded yet.