I have found the answer here Link. If I add enableImplicitConversion: true along with the @Type decorator then it is working as expected.

export class UserDto extends AbstractDto {
    @Expose()
    email: string;

    @Expose()
    first_name: string;

    @Expose()
    last_name: string;

    @Expose()
    @Type(() => ProfileDto)
    profile: ProfileDto

}

Serializer:

const serialized = plainToClass(UserDto, user, {
    excludeExtraneousValues: true,
    enableImplicitConversion: true
    
});
Answer from RAHUL KUNDU on Stack Overflow
๐ŸŒ
npm
npmjs.com โ€บ package โ€บ @nestjs โ€บ class-transformer
nestjs/class-transformer
October 29, 2021 - This method transforms your class object into a new instance of the class object. This may be treated as deep clone of your objects. import { classToClass } from '@nestjs/class-transformer'; let photo = classToClass(photo);
      ยป npm install @nestjs/class-transformer
    
Published ย  Oct 29, 2021
Version ย  0.4.0
๐ŸŒ
Wdk-docs
wdk-docs.github.io โ€บ nest-docs โ€บ techniques โ€บ validator โ€บ class-transformer
class-transformer - NestJS
Its ES6 and Typescript era. Nowadays you are working with classes and constructor objects more than ever. Class-transformer allows you to transform plain object to some instance of class and versa. Also it allows to serialize / deserialize object based on criteria.
Discussions

typescript - serialize nested objects using class-transformer : Nest js - Stack Overflow
This question is not related to NestJS, but purely to class-transformer. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How is the class transformer DTO in NestJS being applied at runtime?
I'm messing around with NestJS and class-validators for the first time. In that context I met something I didn't quite understand, and haven't been able to find an explanation online. I'm strugglin... More on stackoverflow.com
๐ŸŒ stackoverflow.com
fix: When I use `plainToClass` in my nestjs app to validate nested env it cannot convert them
Description I am using class-validator and class-transformer to validate my .env but it cannot validate nested objects even though I am using @type(() => ClassName) in my code. But at the same t... More on github.com
๐ŸŒ github.com
8
April 28, 2023
nestjs - How to transform nested object with class-transformer's @Transform - Stack Overflow
You need to add a property to the transformation decorator toClassOnly. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
DEV Community
dev.to โ€บ jay818 โ€บ nest-js-class-validator-class-transformers-3ppg
Nest JS Class Validator & Class Transformers - DEV Community
May 28, 2025 - app.useGlobalPipe(new ValidationPipe({transform:true})); They are used when we have to validate Nested fields. class UserDTO{ @IsString() @IsNotEmpty() name:string; } class SendMessageDTO{ @IsString(); @IsNotEmpty() message:string; @ValidateNested() ...
Top answer
1 of 2
26

I have found the answer here Link. If I add enableImplicitConversion: true along with the @Type decorator then it is working as expected.

export class UserDto extends AbstractDto {
    @Expose()
    email: string;

    @Expose()
    first_name: string;

    @Expose()
    last_name: string;

    @Expose()
    @Type(() => ProfileDto)
    profile: ProfileDto

}

Serializer:

const serialized = plainToClass(UserDto, user, {
    excludeExtraneousValues: true,
    enableImplicitConversion: true
    
});
2 of 2
2

This question is not related to NestJS, but purely to class-transformer. NestJS might happen to use the class-validator & class-transformer packages as part of its pipes feature, but in the context of this question NestJS doesn't even need to be considered.

Let's assume you have two classes, Cat and Owner. An owner can have a cat.

class Cat {
  @Expose()
  name: string;

  @Expose()
  age: number;

  favoriteFood: string;

  constructor(name: string, age: number, favoriteFood: string) {
    this.name = name;
    this.age = age;
    this.favoriteFood = favoriteFood;
  }
}

class Owner {
  @Expose()
  name: string;

  @Expose()
  cat: Cat;

  constructor(name: string, cat: Cat) {
    this.name = name;
    this.cat = cat;
  }
}

Let's instantiate an instance of each.

const cat = new Cat('Misty', 6, 'Dry cat food');
const owner = new Owner('Christophe', cat);

If you want to convert the owner instance back into a plain JavaScript object then use the instanceToPlain() function with the excludeAll strategy from class-transformer. The classToPlain() function is deprecated.

const serialized = instanceToPlain(owner, { strategy: 'excludeAll' });

That will only serialize the properties you decorated with the @Expose() decorator:

{ name: 'Christophe', cat: { name: 'Misty', age: 6 } }

The plainToClass() you used in your example is meant to convert a plain JavaScript back into an instance of the Owner class, or for "deserializing" rather.

For more information consult the class-transformer documentation.

https://github.com/typestack/class-transformer

๐ŸŒ
NestJS
docs.nestjs.com โ€บ techniques โ€บ serialization
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
๐ŸŒ
GitHub
github.com โ€บ nestjs โ€บ class-transformer
GitHub - nestjs/class-transformer: Fork of the class-transformer package. Decorator-based transformation, serialization, and deserialization between objects and classes.
Decorator-based transformation, serialization, and deserialization between objects and classes. - GitHub - nestjs/class-transformer: Fork of the class-transformer package. Decorator-based transformation, serialization, and deserialization between ...
Starred by 59 users
Forked by 5 users
Languages ย  TypeScript 99.6% | JavaScript 0.4% | TypeScript 99.6% | JavaScript 0.4%
Find elsewhere
๐ŸŒ
Michael Guay
michaelguay.dev โ€บ home โ€บ lectures โ€บ nestjs domain driven design with class transformer
NestJS Domain Driven Design With Class Transformer - Michael Guay
March 21, 2026 - Class-transformer can transform regular objects returned from the database or a network call, and turn them into their respective classes with the same data but now with the methods we define to act on that data.
๐ŸŒ
Medium
medium.com โ€บ @zzpzaf.se โ€บ nestjs-rest-api-class-validator-class-transformer-bf7a0e6b311a
NestJS REST API: class-validator & class-transformer | by Panos Zafeiropoulos | Medium
March 14, 2022 - If you have already used Angular before, you will get familiarized with it, almost immediately. It is supposed that you are familiar with the NestJS basic building blocks like Modules, Providers, Controllers, Routers, etc. In this short post, we will try to focus on how to start implementing the class-validator and class-transformer libraries.
๐ŸŒ
Adarsha Acharya
adarsha.dev โ€บ blog โ€บ serialize-response-nestjs
Serialize Nest.js API Response using class-transformer and class-validator | Adarsha Acharya
April 30, 2024 - Instead we will use technique called serialization creating a interceptor to transform the response as per the DTO defined. First we need to install the required packages. ... Now lets create interceptor for the response object. ... import { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { plainToClass } from 'class-transformer'; export interface ClassContrustor { new (...args: any[]): object; } export class SerializeInterceptor implements NestInterceptor { constructor(private dto: ClassCon
๐ŸŒ
Devxperiences
devxperiences.com โ€บ pzwp1 โ€บ 2022 โ€บ 03 โ€บ 14 โ€บ nestjs-rest-api-class-validator-class-transformer
NestJS REST API: class-validator & class-transformer โ€“ DevXperiences
March 14, 2022 - Pipes are classes annotated with the @Injectable() decorator (you can think that they are similar to Providers). Pipes implement the PipeTransform generic interface. Therefore, every pipe must have a transform() method. This method will be called by NestJS to process the arguments.
๐ŸŒ
Medium
medium.com โ€บ @temunel โ€บ unlocking-the-power-of-data-validation-and-transformation-in-nestjs-5e280af2aa76
Unlocking the Power of Data Validation and Transformation in NestJS | by Temunel | Medium
September 28, 2024 - In this article, weโ€™ve covered how to use class-validator and class-transformer to manage data validation and transformation in NestJS applications. These libraries make it easier to keep your code clean, robust, and scalable.
๐ŸŒ
This Dot Labs
thisdot.co โ€บ blog โ€บ combining-validators-and-transformers-in-nestjs
Combining Validators and Transformers in NestJS - This Dot Labs
February 13, 2023 - First, let's install the dependencies needed for using data transformation and validation in NestJS: ... As their names would suggest, the class-validator package brings support for validating data, while the class-transformer package brings support for transforming data.
๐ŸŒ
DevGenius
blog.devgenius.io โ€บ 6-unlocking-the-power-of-data-transformation-in-nestjs-graphql-with-class-transformer-5a743da743bc
#6 Unlocking the Power of Data Transformation in NestJS GraphQL with Class-Transformer | by Bhavy Shekhaliya | Dev Genius
July 24, 2024 - As applications grow more complex, the need to transform and validate data seamlessly becomes crucial. Enter class-transformer, a powerful tool in the NestJS GraphQL ecosystem that simplifies data transformation, making your application robust, maintainable, and efficient.
๐ŸŒ
Medium
medium.com โ€บ @jradzik4 โ€บ data-validation-in-nestjs-with-class-validator-and-class-transformer-ec9e5057e54f
Data Validation in NestJS with class-validator and class-transformer | by Jakub Radzik | Medium
February 5, 2026 - Automatic type transformation: ... with class-validator and class-transformer provides a clean, elegant solution for validating incoming data with minimal boilerplate code....
๐ŸŒ
GitHub
github.com โ€บ typestack โ€บ class-transformer โ€บ issues โ€บ 1519
fix: When I use `plainToClass` in my nestjs app to validate nested env it cannot convert them ยท Issue #1519 ยท typestack/class-transformer
April 28, 2023 - Description I am using class-validator and class-transformer to validate my .env but it cannot validate nested objects even though I am using @type(() => ClassName) in my code. But at the same time I am able to validate incoming http req...
Author ย  typestack
๐ŸŒ
Felix Astner
felixastner.com โ€บ home โ€บ articles โ€บ why nestjs uses dtos and interfaces: unlocking their power with class-transformer
Why NestJS Uses DTOs and Interfaces: Unlocking Their Power with class-transformer | Felix Astner | Felix Astner
May 24, 2025 - The class-transformer library enhances DTOs by converting plain JavaScript objects (e.g., request payloads) into class instances and transforming data (e.g., excluding sensitive fields or renaming properties). Itโ€™s commonly used in NestJS to:
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 77918681 โ€บ how-to-transform-nested-object-with-class-transformers-transform
nestjs - How to transform nested object with class-transformer's @Transform - Stack Overflow
@Transform( ({ value }) => value.id, { toClassOnly: true, }, ) Here's an explanation from the developer https://github.com/nestjs/nest/issues/3842#issuecomment-575041486