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

976
Vistas
How to map with the help of Automapper (TypeScript) the entries of an array filled with different DTOs?

I'm using the TypeScript library "Automapper" in my NestJS backend to convert an answer of MongoDb/Mongoose to an DTO. That's how the answer looks like:

{
    "_id": "mongo-object-id",
    "widgets": [
      {
        "id": "UUID",
        "type": "IMAGE",
        "zIndex": "0",
        "position": { "x": 1, "y": 2 },
        "pinned": false,
        "height":500,
        "width":1000
      },
      {
        "id": "UUID",
        "type": "CONVERSATION",
        "zIndex": "1",
        "position": { "x": 3, "y": 7 },
        "pinned": false,
        "participants": ["UUID1","UUID2"]
      }
    ]
  }

My problem is, that I don't know how to convert the entries of the "widgets" array to different DTOs (ImageDTO, ConversationDTO) based on the "type" attribute/discriminator.

How can I solve that?

Currently I use the following:

mapper.createMap(MeetingSpace, MeetingSpaceDto).forMember(
        (destination) => destination.widgets,
        mapWith(WidgetDto, Widget, (source) => source.widgets)
      );

But I have to find a way to map a "Widget" to an "ImageDto" or "ConversationDto" because of the "type".

mapWith(ImageDto, Widget, (source) => source.widgets)
mapWith(ConversationDto, Widget, (source) => source.widgets)
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I know this is a bit late - but I just came across the same need.

You need to define the mapping from Widget to ImageDTO

createMap(mapper, Widget, ImageDTO);

Then use the map function on the collection:

return source.widgets.map(widget => mapper.map(widget, Widget, ImageDTO);
over 4 years ago · Santiago Trujillo Denunciar
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