What is the main purpose of the class-transformer library in NestJS?
It helps convert plain JavaScript objects into class instances and vice versa, making it easier to work with typed data and apply transformations or validations.
Click to reveal answer
beginner
How do you use @Expose() decorator in class-transformer?
The <code>@Expose()</code> decorator marks a class property to be included when transforming an object to plain or class instance, allowing control over which properties are visible.
Click to reveal answer
beginner
What does the plainToInstance() function do?
It converts a plain JavaScript object into an instance of a specified class, applying any decorators like @Type() or @Expose() during the transformation.
Click to reveal answer
intermediate
Why would you use @Type(() => SomeClass) in a DTO class?
To tell class-transformer how to transform nested objects or arrays into instances of the specified class, ensuring proper typing and transformation of nested data.
Click to reveal answer
intermediate
What is the effect of using excludeExtraneousValues: true option in transformation?
It removes any properties from the plain object that do not have @Expose() decorator in the class, helping to keep only intended data and improve security.
Click to reveal answer
Which function converts a plain object to a class instance in class-transformer?
AplainToInstance()
BclassToPlain()
CtransformObject()
DtoClass()
✗ Incorrect
The plainToInstance() function converts plain objects to class instances.
What does the @Expose() decorator do?
AValidates property values
BHides properties from transformation
CMarks properties to be included during transformation
DConverts property types automatically
✗ Incorrect
@Expose() marks properties to be included when transforming objects.
How do you specify the type of a nested object for transformation?
A@ExposeNested(ClassName)
B@NestedType(ClassName)
C@TransformType(ClassName)
D@Type(() => ClassName)
✗ Incorrect
Use @Type(() => ClassName) to specify nested object types.
What happens if you set excludeExtraneousValues: true during transformation?
AOnly properties with @Expose() are included
BAll properties are included
CTransformation fails if extra properties exist
DProperties are renamed automatically
✗ Incorrect
Only properties decorated with @Expose() are included when excludeExtraneousValues is true.
Which decorator is NOT part of class-transformer?
A@Expose()
B@Validate()
C@Type()
D@Exclude()
✗ Incorrect
@Validate() is from class-validator, not class-transformer.
Explain how you would use class-transformer to convert a plain JSON object into a typed class instance with nested objects in NestJS.
Think about how to prepare your classes and call the right function.
You got /4 concepts.
Describe the benefits of using excludeExtraneousValues: true option in data transformation.
Consider what happens to properties without @Expose() when this option is enabled.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the class-transformer library in a NestJS application?
easy
A. To convert plain JavaScript objects into class instances and vice versa
B. To handle HTTP requests and responses
C. To manage database connections
D. To style the frontend components
Solution
Step 1: Understand the role of class-transformer
The library is designed to transform plain objects into instances of classes and back, which helps in managing data cleanly.
Step 2: Compare with other options
Options B, C, and A relate to other parts of NestJS or frontend, not data transformation.
Final Answer:
To convert plain JavaScript objects into class instances and vice versa -> Option A
Quick Check:
class-transformer = object-class conversion [OK]
Hint: Remember: class-transformer changes plain objects to class instances [OK]
Common Mistakes:
Confusing class-transformer with HTTP or database modules
Thinking it styles frontend components
Assuming it manages server routing
2. Which decorator from class-transformer is used to exclude a property from the transformed output?
easy
A. @Expose()
B. @Exclude()
C. @Transform()
D. @Type()
Solution
Step 1: Identify the decorator for exclusion
The @Exclude() decorator marks properties to be left out during transformation.
Step 2: Understand other decorators
@Expose() includes properties, @Transform() customizes transformation, and @Type() defines property types.
Final Answer:
@Exclude() -> Option B
Quick Check:
Exclude means leave out = @Exclude() [OK]
Hint: Exclude properties with @Exclude() decorator [OK]
Common Mistakes:
Using @Expose() to exclude instead of include
Confusing @Transform() with exclusion
Not importing decorators correctly
3. Given the following code, what will be the output of console.log(userTransformed)?
5. You want to transform a plain object with nested properties into class instances using class-transformer. Which approach correctly handles nested objects?
import { plainToInstance, Type } from 'class-transformer';
class Address {
street: string;
city: string;
}
class User {
name: string;
@Type(() => Address)
address: Address;
}
const plainUser = {
name: 'Bob',
address: { street: 'Main St', city: 'Townsville' }
};
const user = plainToInstance(User, plainUser);
console.log(user.address instanceof Address);
What will console.log output and why?
hard
A. undefined, because address property is missing
B. false, because nested objects are not transformed automatically
C. Error, because @Type decorator is used incorrectly
D. true, because @Type decorator converts nested object to Address instance
Solution
Step 1: Recognize the role of @Type decorator
The @Type(() => Address) decorator tells class-transformer to convert the nested plain object into an Address class instance.
Step 2: Check the output of instanceof
Since transformation happens correctly, user.address is an instance of Address, so the output is true.
Final Answer:
true, because @Type decorator converts nested object to Address instance -> Option D
Quick Check:
@Type enables nested class conversion = true [OK]
Hint: Use @Type(() => Class) for nested object transformation [OK]
Common Mistakes:
Forgetting @Type causes nested objects to stay plain
Assuming nested objects transform automatically
Misusing @Type decorator syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.