Guide on using automapper in typescript to map a complex object to a "Map" or "Record" interface

I have been utilizing the automapper-ts with typescript plugin for automatic mapping. Check it out here

While it works smoothly for simple objects, I encountered issues when dealing with complex ones like: Record<string, any> or Map<string, AnotherObject>.

  1. Here is an example involving Map<string, SampleItem>:
export class SampleItem {
  type: string;
  name: string;
  example?: unknown;

  constructor(data?: SampleItem) {
    Object.assign(this, data);
  }
}

class Entity {
  @AutoMap(() => SampleItem)
  sample: Map<string, SampleItem>;

  @AutoMap(() => Object)
  details: Record<string, any>;
}

This presents a test scenario comparing both situations:

The issue arises from the following payload:

{
   sample: {
      sampleOne: {
        name: 'sample name',
        type: SampleTypes.STRING,
        example: 'sample example',
      } as SampleItem,
    },
    description: 'description'
}

  1. Another complication occurred when integrating a complex object with the details field:
{
    description: { des: 'description' } as Record<string, any>,
}
Original error: Error: Mapping is not found for function String() { [native code] } and function String() { [native code] }

    at setMember (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:379:15)
    at map (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:439:9)
    at mapReturn (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:291:10)
    at Proxy.<anonymous> (/home/fabiofilho/Projects/project/node_modules/@automapper/core/index.cjs:691:31)

Additional information - using the ttypescript:

"dependencies": {
    "@automapper/classes": "^8.7.5",
    "@automapper/core": "^8.7.5",
    "@automapper/mikro": "^8.7.5",
    "@automapper/nestjs": "^8.7.5",
    "@automapper/pojos": "^8.7.5",
    "@automapper/sequelize": "^8.7.5",
}

module.exports = {
  [...]
  globals: {
    'ts-jest': {
      compiler: 'ttypescript',
    },
  },
};

Answer №1

All is well now, I had mistakenly used incorrect parameters to map the object earlier. The fault lies with me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Remove the Prisma self-referencing relationship (one-to-many)

I'm working with this particular prisma schema: model Directory { id String @id @default(cuid()) name String? parentDirectoryId String? userId String parentDirectory Directory? @relation("p ...

Using Typescript to test with Karma, we can inject a service for our

I'm currently experiencing a problem with running tests in my application. I recently transitioned to using TypeScript and am still in the learning process. The specific error I'm encountering is: Error: [$injector:unpr] Unknown provider: movie ...

How to manage type mappings while utilizing the spread syntax

In my testing scenario, I am utilizing a setup function and I am looking for a way to pass typing information along when it is called so that I can benefit from intelligence support without having to bypass it in eslint. function setup(): SomeType { retu ...

Add integer to an array of strings

Currently, I am utilizing an autocomplete feature and aiming to save the IDs of the selected users. My goal is to store these IDs in a string array, ensuring that all values are unique with no duplicates. I have attempted to push and convert the values u ...

Specify the key type when using the syntax "key in ObjectName"

I need assistance in setting the key type of an object. Here is what I have tried: type TypeSample = { [key: string]: string } In addition, I want to specify that the keys should come from an enum like this: enum EnumSample { 'ok' = &a ...

Scroll to the top on every Angular 5 route change

Currently, I am utilizing Angular 5 for my project. Within the dashboard interface, there are various sections with varying amounts of content. Some sections contain only a small amount of information, while others have large amounts of content. However, w ...

Discover the solution for seamless integration of TypeScript with the novel `exports` and `main` field

I am currently utilizing Node.js version 16.10.0 along with TypeScript 4.5.5. As part of my development process, I am in the midst of publishing a library and have implemented the following configuration: "main": "./dist/index.js", ...

Is Joi's existence a myth?

I've been using Joi for validation, and I've encountered a situation that I'm having trouble with. I have an object that sometimes includes an id field (for editing) and other times it doesn't (for creating). My goal is to validate tha ...

Clear drop down selections after button is pressed

I am currently working with a grid in my template that contains multiple dropdowns, each row having its own. When I click a button, I gather the values from these dropdowns. However, upon clicking this button, I wish to reset all the dropdowns back to thei ...

TypeScript integration for express-validator

Recently, I made an attempt to switch my NodeJS project with ExpressJS to TypeScript for better organization and type safety. However, I encountered an issue with the 'express-validator' middleware during this conversion process. To resolve thi ...

Struggling to comprehend the intricacies of these generic declarations, particularly when it comes to Type Argument Lists

I'm currently reviewing the code snippet from the TypeScript definitions of fastify. I am struggling to understand these definitions. Although I am familiar with angle brackets used for generics, most TypeScript tutorials focus on simple types like Ar ...

Match and populate objects from the array with corresponding items

Currently, I have an array and object containing items, and my goal is to check each item in the array to see if its path matches any of the object names. If a match is found, I push it into that object's array. While this part is working fine, I am ...

The 'innerText' property is not found in the 'Element' type

Currently, I am working with Typescript and Puppeteer. My goal is to extract the innerText from an element. const data = await page.$eval(selector, node => node.innerText); However, I encountered an error: The property 'innerText' is not ...

Upgrade your project from Angular 5 to Angular 9 using webpack

I want to share my experience, not ask a question! To upgrade dependencies in package.json: -Update all Angular dependencies to version 9 -Add these dependencies: "@angular-devkit/build-angular": "^0.900.4", "@angular-builders/cu ...

Developing a separate NPM package for a portion of the app causes an increase in the total app size

Our projects utilize create-react-app and Typescript, resulting in the emergence of a collection of commonly used React components. To enhance maintenance and reusability, we decided to extract these components into their own NPM package named PackageA. Su ...

Steps to execute an Angular directory within a project

Click here for imageWhenever I try to run ng serve command from the directory, it doesn't seem to work and just takes me back to the same directory. I'm having trouble running my Angular project. Any suggestions on how to solve this issue? ...

Configuring TypeScript for Firefox to recognize specific types such as browser.storage

As per the documentation from Mozilla, I should be able to utilize browser.storage.sync.get in my extension. However, I am encountering some challenges in getting TypeScript to recognize that I can use browser. I have attempted the following (which has wo ...

The not-null constraint is violated in the "id" column because of a null value when using Sequelize Typescript

My Database Setup Journey Recently, I embarked on a database adventure where I decided to use Sequelize-Typescript to assist me with all the heavy lifting in terms of database operations. The first step was creating a table called uma_tbl_users, and here ...

The Redux Toolkit slice and TypeScript were not in agreement: it was expecting 0 arguments, but received

Recently, I encountered an issue with my slice code: const investment = createSlice({ name: 'investments', initialState, reducers: { getInvestmentsRequest(state) { state.investments.status = RequestStatuses.loading; }, } }) ...

You cannot use .addCursorFlag() with Mongoose Typescript

Here is my mongoose model that retrieves data from the database using a cursor. The cursor has a timeout of 10 minutes as per the documentation. const cursor = this.importRecordModel.find().cursor() I attempted to add the following code at the end of the ...