Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
Bridge between incompatible implementations in typescript

I'm trying to create a bridge between two services with an incompatible interface.

ServiceType is an argument from the user, so it has to be validated during runtime.

The best I could think of is this. But there are several problems. I will have to create a similar method like find on the SericeByType object for each function on CampaignService | LeadService and the result from that function is a union of CampaignModel | LeadModel.

Is there a better way how to do it?

interface CampaignModel {
  id: number
}

class CampaignService {
  public async find(startId: number, limit: number): Promise<CampaignModel[]> {
    return [];
  }
}

interface LeadModel {
  id: string
}

class LeadService {
  public async find(startId: string, limit: number): Promise<LeadModel[]> {
    return [];
  }
}

type ServiceType = 'Campaign' | 'Lead'

class SericeByType {
  private readonly campaignService: CampaignService = new CampaignService();
  private readonly leadService: LeadService = new LeadService();

  public async find(type: ServiceType, startId: number | string, limit: number) {
    if (this.isCampaign(type)) {
      return this.campaignService.find(startId as number, limit);
    } else if (this.isLead(type)) {
      return this.leadService.find(startId as string, limit);
    } else {
      throw new Error();
    }
  }

  private isCampaign(type: ServiceType) {
    return type === 'Campaign';
  }

  private isLead(type: ServiceType) {
    return type === 'Lead';
  }
}

const service = new SericeByType();
service.find('Campaign', 16, 10);
service.find('Lead', 'xxxawf', 10);

Playground

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda