0
0
Typescriptprogramming~5 mins

Mapped type for deep transformations in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a mapped type in TypeScript?
A mapped type creates a new type by transforming each property of an existing type. It uses syntax like { [K in keyof T]: ... } to loop over keys.
Click to reveal answer
intermediate
How can you apply a transformation deeply to nested objects using mapped types?
You can use recursive mapped types that check if a property is an object and then apply the mapped type again to that property, enabling deep transformations.
Click to reveal answer
advanced
Explain the purpose of the infer keyword in deep mapped types.
infer helps extract types inside conditional types. It can be used to capture inner types during recursion for deep transformations.
Click to reveal answer
intermediate
What does this TypeScript type do?<br>
type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K] };
It makes all properties of T deeply readonly, meaning nested objects' properties are also readonly recursively.
Click to reveal answer
intermediate
Why do we need to check if a property is an object before applying deep mapped types?
Because primitive types like string or number should not be transformed recursively. Checking prevents errors and infinite recursion.
Click to reveal answer
What does keyof T represent in a mapped type?
AA primitive type
BAll values of type T
CA function in T
DAll keys of type T
Which TypeScript feature allows a mapped type to apply itself recursively?
AEnums
BConditional types
CInterfaces
DType assertions
What is the main risk if you don't check for object types in deep mapped types?
AInfinite recursion
BSyntax error
CTypeScript crashes
DNo effect
What does the readonly modifier do in a mapped type?
AMakes properties optional
BDeletes properties
CPrevents properties from being changed
DChanges property types
Which syntax correctly defines a mapped type?
A{ [K in keyof T]: T[K] }
Bfunction map(T) { return T; }
Cinterface Mapped<T> {}
Dtype Mapped = T[]
Describe how to create a deep transformation mapped type in TypeScript.
Think about looping over keys and applying the type again if the property is an object.
You got /4 concepts.
    Explain why deep mapped types are useful in real-world TypeScript projects.
    Consider how nested data structures benefit from consistent type changes.
    You got /4 concepts.