0
0
Typescriptprogramming~20 mins

Mapped type modifiers (readonly, optional) in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mapped Type Modifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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?
ACompilation error: Cannot assign to 'name' because it is a read-only property.
BNo error, p.name is changed to 'Bob'.
CRuntime error: Cannot assign to read-only property 'name'.
DCompilation error: Property 'name' does not exist on type 'ReadonlyPerson'.
Attempts:
2 left
💡 Hint
Readonly mapped types make all properties immutable at compile time.
Predict Output
intermediate
2: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" };
A{ name?: string; age: number; }
B{ name: string; age: number; }
C{ name?: string | undefined; age?: number | undefined; }
D{ name: string | undefined; age: number | undefined; }
Attempts:
2 left
💡 Hint
The optional modifier adds '?' to all properties.
🔧 Debug
advanced
2: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];
};
Atype ModifiedFlags = { +readonly -? [K in keyof Flags]; Flags[K]; };
Btype ModifiedFlags = { readonly [K in keyof Flags]-?: Flags[K]; };
Ctype ModifiedFlags = { +readonly [K in keyof Flags]-?: Flags[K] };
Dtype ModifiedFlags = { +readonly [K in keyof Flags]-?: Flags[K]; };
Attempts:
2 left
💡 Hint
Check the syntax for mapped type modifiers and property declarations.
Predict Output
advanced
2: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];
};
A{ id?: number; name?: string; }
B{ readonly id?: number | undefined; readonly name?: string | undefined; }
C{ readonly id: number; readonly name: string; }
D{ id: number; name: string; }
Attempts:
2 left
💡 Hint
Both readonly and optional modifiers apply to all properties.
🧠 Conceptual
expert
3: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];
};
ARemoves optional but keeps properties readonly.
BAdds readonly and optional modifiers, making all properties immutable and optional.
CRemoves readonly but keeps properties optional.
DRemoves readonly and optional modifiers, making all properties mutable and required.
Attempts:
2 left
💡 Hint
Minus modifiers remove the specified property modifiers.