Skip to main content

Create Mappings

Our models have been prepped from the last step. It is time to create the Mapping between them.


Mappings creation are fast and should be placed where the application starts so that they are only created once.


import { createMap } from '@automapper/core';
import { mapper } from './mappings/mapper';
import { Bio, BioDto } from './models/bio';
import { User, UserDto } from './models/user';

createMap(mapper, Bio, BioDto);
createMap(mapper, User, UserDto);

Order of createMap() matters. Since Bio and BioDto are nested models on User and UserDto, the Mapping<Bio, BioDto> needs to be created first in order for Mapping<User, UserDto> to be initialized correctly.

createMap(mapper, Source, Destination) will create and return a Mapping<Source, Destination>. Let's test the Mappings out with the following User

const user = new User();
user.firstName = 'Chau';
user.lastName = 'Tran';
user.username = 'ctran';
user.password = '123456'; = new Bio(); = ''; = new Date(); = new Job(); = 'Developer'; = 99999;


To execute a map operation from User to UserDto, you invoke:

const dto =, User, UserDto);

You will notice that the call runs successfully but with two errors logged to the console.

Unmapped properties for Bio -> BioDto:

Unmapped properties for User -> UserDto:

Let's also take a look at the dto

UserDto {
bio: BioDto {
avatarUrl: '',
birthday: 2022-03-23T22:46:17.110Z
username: 'ctran',
lastName: 'Tran',
firstName: 'Chau'

It is clear that bio.jobTitle, bio.jobSalary, and fullName are missing. The errors are correct. Additionally, there's also another "error": UserDto#birthday is a string but it holds a Date value now because it was mapped from User#birthday.

Let's fix the issues one by one in the next section.