Recall & Review
beginner
What does the
readonly modifier do in a mapped type?It makes all properties in the mapped type read-only, meaning their values cannot be changed after initialization.
Click to reveal answer
beginner
How do you make all properties optional in a mapped type?
By adding the
? modifier after the property name in the mapped type, like [P in keyof T]?: T[P].Click to reveal answer
intermediate
What is the effect of using
+readonly and -readonly in mapped types?+readonly adds the readonly modifier to properties, while -readonly removes it if it was present.Click to reveal answer
intermediate
Explain the difference between
+? and -? in mapped types.+? makes properties optional, -? makes them required (removes optional).Click to reveal answer
beginner
Show a simple example of a mapped type that makes all properties readonly and optional.
type ReadonlyOptional<T> = { readonly [P in keyof T]?: T[P] };
Click to reveal answer
What does
{ readonly [P in keyof T]: T[P] } do?✗ Incorrect
The
readonly modifier before the property makes all properties readonly.How do you remove the optional modifier from all properties in a mapped type?
✗ Incorrect
-? removes the optional modifier, making properties required.What does
{ -readonly [P in keyof T]: T[P] } do?✗ Incorrect
-readonly removes the readonly modifier from properties.Which syntax makes all properties optional in a mapped type?
✗ Incorrect
Adding
? after the property name makes properties optional.What is the result of
{ readonly [P in keyof T]?: T[P] }?✗ Incorrect
The
readonly modifier makes properties readonly, and ? makes them optional.Explain how to use mapped type modifiers to make all properties readonly and optional in TypeScript.
Think about adding +readonly and ? in the mapped type.
You got /4 concepts.
Describe the difference between + and - modifiers in mapped types for readonly and optional properties.
Consider how you can add or remove modifiers explicitly.
You got /5 concepts.