0Pricing
TypeScript Academy · Lesson

Constructor Injection

Inject dependencies through constructors cleanly.

Constructor Injection in Depth

Constructor injection declares every dependency as a constructor parameter. The object is fully wired the moment it exists, which makes required collaborators impossible to forget.

Parameter Properties

TypeScript can declare and assign a dependency in one step using an access modifier on the parameter. private readonly creates and assigns the field automatically.

interface Mailer { send(to: string, body: string): void; }
class Notifier {
  constructor(private readonly mailer: Mailer) {}
}

All lessons in this course

  1. Inversion of Control Basics
  2. Constructor Injection
  3. DI Containers with InversifyJS
  4. Type-Safe Service Tokens
← Back to TypeScript Academy