0
0
NestJSframework~5 mins

class-transformer usage in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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()
What does the @Expose() decorator do?
AValidates property values
BHides properties from transformation
CMarks properties to be included during transformation
DConverts property types automatically
How do you specify the type of a nested object for transformation?
A@ExposeNested(ClassName)
B@NestedType(ClassName)
C@TransformType(ClassName)
D@Type(() => ClassName)
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
Which decorator is NOT part of class-transformer?
A@Expose()
B@Validate()
C@Type()
D@Exclude()
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.