DI Containers with InversifyJS
Register and resolve services with a container.
DI Containers with InversifyJS is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why a DI Container?
Manually wiring large object graphs is tedious and error-prone. A DI container registers how to build each dependency and resolves them automatically. InversifyJS is a popular TypeScript container.
Installing and Enabling
InversifyJS relies on decorators and reflection metadata. You import from inversify and enable experimentalDecorators plus reflect-metadata.
import "reflect-metadata";
import { Container, injectable, inject } from "inversify";Marking Classes @injectable
The @injectable() decorator tells the container a class can be constructed and injected. Every class the container builds needs it.
import { injectable } from "inversify";
@injectable()
class ConsoleLogger {
log(m: string) { console.log(m); }
}Declaring Dependencies with @inject
Use @inject(token) on constructor parameters to tell the container which binding to supply. The token identifies the dependency.
@injectable()
class OrderService {
constructor(@inject("Logger") private logger: ConsoleLogger) {}
place() { this.logger.log("placed"); }
}Creating a Container
A Container holds all the bindings for your application. You create one at startup and register dependencies on it.
import { Container } from "inversify";
const container = new Container();Binding an Interface to an Impl
The fluent API bind<T>(token).to(Impl) maps a token (often an interface) to a concrete class the container will instantiate.
interface Logger { log(m: string): void; }
container.bind<Logger>("Logger").to(ConsoleLogger);Resolving Services
container.get<T>(token) builds the requested service, recursively constructing and injecting its dependencies for you.
container.bind<OrderService>(OrderService).toSelf();
const svc = container.get<OrderService>(OrderService);
svc.place();Swapping Implementations
To use a different logger, rebind the token to another class. Consumers are untouched because they depend on the token, not the concrete class.
@injectable()
class SilentLogger implements Logger { log() {} }
container.rebind<Logger>("Logger").to(SilentLogger);Singleton Scope
By default each get may build a new instance. Use inSingletonScope() to share one instance across the app.
container.bind<Logger>("Logger").to(ConsoleLogger).inSingletonScope();Testing with the Container
In tests you can build a fresh container and bind fakes, getting the same wiring benefits while isolating the unit under test.
const testContainer = new Container();
testContainer.bind<Logger>("Logger").to(SilentLogger);Container Trade-offs
Containers reduce boilerplate for big graphs but add a dependency, decorators, and indirection. For small apps, manual constructor injection is often clearer.
Quick Check
Quick check on this lesson.
Recap
InversifyJS uses @injectable, @inject(token), and a Container. You bind<Interface>(token).to(Impl) and container.get resolves the graph automatically. It needs reflect-metadata and decorators, trading boilerplate for indirection.
Frequently asked questions
Is the “DI Containers with InversifyJS” lesson free?
Yes — the full text of “DI Containers with InversifyJS” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “DI Containers with InversifyJS”?
Register and resolve services with a container. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “DI Containers with InversifyJS” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Inversion of Control Basics
- Constructor Injection
- DI Containers with InversifyJS
- Type-Safe Service Tokens