Dependency Injection
Dependency injection means a type receives its collaborators as constructor arguments (or parameters) instead of importing concrete instances. TypeScript types those collaborators as interfaces so tests can pass fakes.
Detailed Explanation
The pattern is ordinary: constructor(private users: UserRepo). The TypeScript part is what UserRepo is.
If UserRepo is a class, the constructor demands that class (or a subclass). Tests have to extend it or fight private members. If UserRepo is an interface (find(id: string): Promise<User | undefined>), any object with that method is valid. A test passes { find: async () => ada }. Production passes new PostgresUserRepo(...). That is structural typing used as DI.
Parameter properties make the field declaration short. Visibility should be private unless subclasses are a supported extension point. The emitted JavaScript is still this.users = users. There is no container unless you add one.
Frameworks (Nest, Inversify, tsyringe) add decorators and tokens because interfaces are erased. You cannot @Inject(UserRepo) if UserRepo is only a type — there is no runtime token. Those libraries ask you to use a class, or a string/symbol token plus an interface. That is a direct consequence of erasure, not a failure of the pattern.
A lightweight typed container is a function: function makeApp(deps: { users: UserRepo; clock: Clock }). Call it from main with concretes, and from tests with fakes. The deps object type is the composition root's contract. Generics appear when the same factory builds multiple apps (makeApp<R extends UserRepo>(...)) — usually unnecessary if the interface is already the bound.
Do not inject any. Do not type a repo as a concrete class and then as UserRepo at the edge to "make it testable"; change the parameter type. Do not put the container in a global typed as Map<string, any>.
DI does not require a framework. In TypeScript, an interface plus a constructor parameter is the whole pattern for most apps.
Code Examples
1. Inject an interface, not a class.
// 1. Inject an interface, not a class.
type User = { id: string; email: string };
export interface UserRepo {
find(id: string): Promise<User | undefined>;
}
export class UserService {
constructor(private readonly users: UserRepo) {}
async emailOf(id: string): Promise<string | undefined> {
return (await this.users.find(id))?.email;
}
}2. A fake in a test — structural.
// 2. A fake in a test — structural.
const fake: UserRepo = {
async find(id) {
return id === "u1" ? { id, email: "ada@example.com" } : undefined;
},
};
const service = new UserService(fake);3. Composition root as a typed object.
// 3. Composition root as a typed object.
type Clock = { now(): Date };
function makeApp(deps: { users: UserRepo; clock: Clock }): UserService {
return new UserService(deps.users);
}Common Gotchas
Interfaces cannot be used as runtime inject tokens
a decorator-based container needs a class or a `symbol`. That is erasure.
Injecting a concrete class couples tests to its constructor and privates
extract an interface at the first test, not the third.
Parameter properties are the usual spelling
`constructor(private readonly users: UserRepo)`.