Copy interface CustomerDto {
firstName : string ;
lastName : string ;
}
interface Customer {
firstName : string ;
lastName : string ;
fullName : string ;
}
TypeScript static typings are not available at runtime. To introduce a runtime representation of mapping from CustomerDto
to Customer
a MappingPair
token has to be created.
Copy import { MappingPair } from '@dynamic-mapper/mapper' ;
const CustomerDtoToCustomer = new MappingPair < CustomerDto , Customer >();
Copy import { MapperConfiguration } from '@dynamic-mapper/mapper' ;
const configuration = new MapperConfiguration (cfg => {
cfg .createAutoMap (CustomerDtoToCustomer , {
fullName : opt => opt .mapFrom (src => ` ${ src .firstName } ${ src .lastName } ` )
});
});
Copy const mapper = configuration .createMapper ();
const customer : CustomerDto = {
firstName : 'John' ,
lastName : 'Doe'
};
const dto = mapper .map (CustomerDtoToCustomer , customer);
console .log (dto);
// { firstName: 'John', lastName: 'Doe', fullName: 'John Doe' }