Skip to content

Duplicate Interfaces

export interface INotificationService {
sendNotification(message: string, recipient: string): Promise<void>;
}
// Multiple implementations of the same interface
export class EmailNotificationService implements INotificationService {
async sendNotification(message: string, recipient: string) { /* ... */ }
}
export class SmsNotificationService implements INotificationService {
async sendNotification(message: string, recipient: string) { /* ... */ }
}
export class PushNotificationService implements INotificationService {
async sendNotification(message: string, recipient: string) { /* ... */ }
}

The Problem: When resolving INotificationService, which implementation should the container use?

Terminal window
Error: Interface 'INotificationService' is implemented by multiple classes:
- EmailNotificationService (/path/to/services/EmailNotificationService.ts)
- SmsNotificationService (/path/to/services/SmsNotificationService.ts)
- PushNotificationService (/path/to/services/PushNotificationService.ts)
Multiple classes implement the same interface(s): INotificationService.
Each interface should only be implemented by one class for proper dependency injection.

Create specific interfaces:

// Base interface
export interface INotificationService {
sendNotification(message: string, recipient: string): Promise<void>;
}
// Specific interfaces
export interface IEmailNotificationService extends INotificationService {}
export interface ISmsNotificationService extends INotificationService {}
export interface IPushNotificationService extends INotificationService {}
// Implementations
export class EmailNotificationService implements IEmailNotificationService {
async sendNotification(message: string, recipient: string) { /* ... */ }
}
export class SmsNotificationService implements ISmsNotificationService {
async sendNotification(message: string, recipient: string) { /* ... */ }
}
export class PushNotificationService implements IPushNotificationService {
async sendNotification(message: string, recipient: string) { /* ... */ }
}

Usage:

import { container } from './container.gen';
// ✅ Each interface has one implementation - no ambiguity!
const emailService = container.resolve('IEmailNotificationService');
const smsService = container.resolve('ISmsNotificationService');
const pushService = container.resolve('IPushNotificationService');