Challenge - 5 Problems
class-transformer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this class-transformer code?
Given the following NestJS code using class-transformer, what will be the output of the console.log?
NestJS
import { plainToInstance } from 'class-transformer'; class User { id: number; name: string; password: string; } const plainUser = { id: 1, name: 'Alice', password: 'secret' }; const userInstance = plainToInstance(User, plainUser, { excludeExtraneousValues: true }); console.log(userInstance);
Attempts:
2 left
💡 Hint
Check if any decorators like @Expose or @Exclude are used to filter properties.
✗ Incorrect
Since no @Expose or @Exclude decorators are used, excludeExtraneousValues has no effect. All properties from plainUser are assigned to the User instance.
🧠 Conceptual
intermediate1:30remaining
What does the @Exclude decorator do in class-transformer?
In class-transformer, what is the effect of adding the @Exclude() decorator to a class property?
Attempts:
2 left
💡 Hint
Think about hiding sensitive data when sending responses.
✗ Incorrect
The @Exclude decorator hides the property when converting class instances to plain objects, useful for removing sensitive fields like passwords.
❓ Hyperparameter
advanced2:30remaining
Which option correctly enables transformation of nested objects with class-transformer?
You have a User class with an Address property which is another class. Which option correctly transforms nested plain objects into class instances?
NestJS
import { plainToInstance } from 'class-transformer'; class Address { street: string; city: string; } class User { name: string; address: Address; } const plainUser = { name: 'Bob', address: { street: '123 Main St', city: 'Townsville' } }; const userInstance = plainToInstance(User, plainUser, OPTIONS_HERE); console.log(userInstance.address instanceof Address);
Attempts:
2 left
💡 Hint
Look for the option that allows automatic conversion of nested objects.
✗ Incorrect
enableImplicitConversion allows class-transformer to convert nested plain objects into instances of their respective classes automatically.
❓ Metrics
advanced2:00remaining
What is the effect of using @Transform decorator in class-transformer?
Consider this code snippet using @Transform on a property. What does the @Transform decorator do?
NestJS
import { Transform, plainToInstance } from 'class-transformer'; class Product { @Transform(({ value }) => value.toUpperCase()) name: string; } const plainProduct = { name: 'laptop' }; const productInstance = plainToInstance(Product, plainProduct); console.log(productInstance.name);
Attempts:
2 left
💡 Hint
Check what the function inside @Transform does to the value.
✗ Incorrect
The @Transform decorator applies a function to the property value during transformation, here converting it to uppercase.
🔧 Debug
expert3:00remaining
Why does this class-transformer code fail to exclude the password property?
Given this code, why is the password property still present in the output?
NestJS
import { plainToInstance, Exclude } from 'class-transformer'; class User { id: number; name: string; @Exclude() password: string; } const plainUser = { id: 1, name: 'Eve', password: '1234' }; const userInstance = plainToInstance(User, plainUser, { excludeExtraneousValues: true }); console.log(userInstance);
Attempts:
2 left
💡 Hint
Check how excludeExtraneousValues interacts with @Expose and @Exclude decorators.
✗ Incorrect
excludeExtraneousValues removes properties not decorated with @Expose. @Exclude alone does not remove properties unless @Expose is used on others.