Skip to content

fix: toClassOnly still expose the name in instanceToPlain #1324

Open
@oldriverno1

Description

@oldriverno1

I have a DTO class and I want some specific key to be covert with different name only in instanceToPlain operation

the dto class:

export class CreateEventDto {
  @Expose({ toClassOnly: true, name: 'type' }) 
  eventTypeId: number;
}

I can get it to work in plainToInstance
for example:

const obj = { type: 2 };
console.log(plainToInstance(CreateEventDto, obj));  // log CreateEventDto { eventTypeId: 2 }

but if I convert it again using instanceToPlain, the result is showing { type: 2 } instead of { eventTypeId: 2 }

const obj = { type: 2 };
const dto = plainToInstance(CreateEventDto, obj);
console.log(dto);  // log CreateEventDto { eventTypeId: 2 }
console.log(instanceToPlain(dto)); // log { type: 2 }, but I expect it to be { eventTypeId: 2 }

Is there anything I'm doing wrong? or any proper way to this kind of conversion?
Thanks for answering :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions