Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

969
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!