Challenge - 5 Problems
Mapped Type Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a mapped type with readonly modifier
What is the type of
ReadonlyPerson after applying the mapped type with readonly modifier?Typescript
type Person = { name: string; age: number };
type ReadonlyPerson = {
readonly [K in keyof Person]: Person[K];
};
const p: ReadonlyPerson = { name: "Alice", age: 30 };
p.name = "Bob"; // What happens here?Attempts:
2 left
💡 Hint
Readonly mapped types make all properties immutable at compile time.
✗ Incorrect
The readonly modifier in a mapped type makes all properties read-only. Trying to assign a new value to p.name causes a compile-time error.
❓ Predict Output
intermediate2:00remaining
Effect of optional modifier in mapped types
Given the mapped type below, what is the type of
PartialPerson?Typescript
type Person = { name: string; age: number };
type PartialPerson = {
[K in keyof Person]?: Person[K];
};
const p: PartialPerson = { name: "Alice" };Attempts:
2 left
💡 Hint
The optional modifier adds '?' to all properties.
✗ Incorrect
The ? in the mapped type makes all properties optional, so both name and age become optional with their original types.
🔧 Debug
advanced2:00remaining
Identify the error in mapped type modifiers usage
Which option contains a syntax error in the mapped type modifiers usage?
Typescript
type Flags = { a: boolean; b: boolean };
type ModifiedFlags = {
+readonly [K in keyof Flags]-?: Flags[K];
};Attempts:
2 left
💡 Hint
Check the syntax for mapped type modifiers and property declarations.
✗ Incorrect
Option A is invalid because it uses a semicolon instead of a colon between the property name and type, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Combined readonly and optional modifiers in mapped types
What is the resulting type of
ReadonlyPartial?Typescript
type User = { id: number; name: string };
type ReadonlyPartial = {
readonly [K in keyof User]?: User[K];
};Attempts:
2 left
💡 Hint
Both readonly and optional modifiers apply to all properties.
✗ Incorrect
The mapped type applies readonly and ? to each property, making them read-only and optional.
🧠 Conceptual
expert3:00remaining
Understanding the effect of removing modifiers in mapped types
Given the type below, what does the
-readonly and -? modifiers do in the mapped type?Typescript
type Config = { readonly host?: string; readonly port?: number };
type MutableRequiredConfig = {
-readonly [K in keyof Config]-?: Config[K];
};Attempts:
2 left
💡 Hint
Minus modifiers remove the specified property modifiers.
✗ Incorrect
The -readonly removes the readonly modifier, and -? removes the optional modifier, so all properties become mutable and required.