Mapped type modifiers let you change properties of an object type all at once. You can make all properties readonly or optional easily.
Mapped type modifiers (readonly, optional) in Typescript
type NewType = {
readonly [Key in keyof OldType]: OldType[Key];
};
// or
type NewType = {
[Key in keyof OldType]?: OldType[Key];
};readonly before the property makes it unchangeable.
? after the property name makes it optional.
Person readonly.type Person = {
name: string;
age: number;
};
type ReadonlyPerson = {
readonly [K in keyof Person]: Person[K];
};Person optional.type PartialPerson = {
[K in keyof Person]?: Person[K];
};type ReadonlyPartialPerson = {
readonly [K in keyof Person]?: Person[K];
};This program shows how to make all properties readonly and optional using mapped type modifiers. It also shows that trying to change a readonly property causes an error.
type Person = {
name: string;
age: number;
};
type ReadonlyPerson = {
readonly [K in keyof Person]: Person[K];
};
const person: ReadonlyPerson = {
name: "Alice",
age: 30
};
// person.name = "Bob"; // Error: Cannot assign to 'name' because it is a read-only property
console.log(person.name);
// Now optional example
type PartialPerson = {
[K in keyof Person]?: Person[K];
};
const partialPerson: PartialPerson = {
name: "Charlie"
};
console.log(partialPerson.age);Readonly properties cannot be changed after creation.
Optional properties may be missing or undefined.
You can combine readonly and ? to make properties both readonly and optional.
Mapped type modifiers let you change all properties of a type at once.
readonly makes properties unchangeable.
? makes properties optional.
Practice
What does the readonly modifier do when used in a mapped type in TypeScript?
Solution
Step 1: Understand the
Thereadonlymodifierreadonlykeyword in TypeScript means the property cannot be changed after initialization.Step 2: Apply to mapped types
When used in a mapped type, it makes all properties of the type unchangeable.Final Answer:
It makes all properties unchangeable (immutable). -> Option DQuick Check:
readonly= immutable [OK]
- Confusing readonly with optional
- Thinking readonly removes properties
- Assuming readonly allows changes
Which of the following is the correct syntax to make all properties optional in a mapped type?
type PartialType<T> = { ? }Solution
Step 1: Recall optional property syntax
In TypeScript, adding?after a property name makes it optional.Step 2: Apply optional modifier in mapped type
The correct syntax is[P in keyof T]?: T[P]to make all properties optional.Final Answer:
type PartialType<T> = { [P in keyof T]?: T[P]; } -> Option AQuick Check:
Optional properties use?:[OK]
- Placing ? after the type instead of property
- Using -? which removes optional
- Confusing readonly with optional
What is the output type of the following code?
type User = { name: string; age: number };
type ReadonlyUser = { readonly [K in keyof User]: User[K] };
const user: ReadonlyUser = { name: "Alice", age: 30 };
user.age = 31;Solution
Step 1: Understand the mapped type with readonly
The typeReadonlyUsermakes all properties ofUserreadonly.Step 2: Check assignment to readonly property
Trying to assignuser.age = 31causes a compile-time error becauseageis readonly.Final Answer:
Error: Cannot assign to 'age' because it is a read-only property. -> Option CQuick Check:
Readonly properties cannot be assigned new values [OK]
- Assuming readonly allows updates
- Thinking property disappears
- Confusing runtime error with compile error
Identify the error in this mapped type that tries to make all properties optional:
type OptionalType<T> = { [P in keyof T]-?: T[P]; };Solution
Step 1: Understand
The-?modifier-?removes the optional modifier from properties, making them required.Step 2: Check effect on properties
Since the goal was to make properties optional, using-?is the opposite and causes the error.Final Answer:
The-?removes optional, so properties are required, not optional. -> Option BQuick Check:
-?removes optional [OK]
- Confusing -? with readonly
- Thinking -? adds optional
- Ignoring the effect of modifiers
Given the type below, which mapped type correctly makes all properties both readonly and optional?
type Config = { url: string; timeout: number; debug: boolean };Solution
Step 1: Combine readonly and optional modifiers
To make properties readonly and optional, usereadonlybefore the property and?after the property name.Step 2: Analyze each option
type ReadonlyOptional<T> = { readonly [P in keyof T]?: T[P] } correctly placesreadonlybefore and?after the property name. Others misuse readonly or optional placement.Final Answer:
type ReadonlyOptional<T> = { readonly [P in keyof T]?: T[P] } -> Option AQuick Check:
readonly before, optional after property name [OK]
- Placing readonly after the type
- Using -? which removes optional
- Confusing readonly with optional placement
Ready to prove it?
Pass the test and this pattern moves unknown
