Aller au contenu

Field Injection (ARCH3200)

Ce contenu n’est pas encore disponible dans votre langue.

Key typescript.quality.field-injection · Severity warning · Category quality

Field/property injection hides dependencies, breaks immutability, and makes the class hard to instantiate without the DI container, lowering testability. Dependencies should be taken through the constructor.

class InvoiceService {
@Inject() private mailer!: Mailer;
send() { this.mailer.deliver(); }
}
class InvoiceService {
constructor(private readonly mailer: Mailer) {}
send() { this.mailer.deliver(); }
}

Inject dependencies through readonly constructor parameters instead of field annotations.