Combining Modifier Changes
What you'll learn: How to apply both readonly and optional (?) modifier changes simultaneously in a single mapped type transformation.
So far you've seen how to manipulate readonly and optional modifiers individually using + and - prefixes. But mapped types become truly powerful when you combine both types of modifier changes in one transformation—turning properties simultaneously readonly and optional, or making them both mutable and required.
The Core Idea
TypeScript allows you to modify both aspects of a property's signature within a single mapped type:
type LockedDefaults<T> = {
readonly [K in keyof T]?: T[K];
};
This type makes every property both readonly and optional—useful for configuration objects you want to freeze and partially apply.
The Syntax Pattern
// Make everything readonly AND required
type Frozen<T> = {
+readonly [K in keyof T]-?: T[K];
};
// Make everything mutable AND optional
type Flexible<T> = {
-readonly [K in keyof T]+?: T[K];
};
Notice the modifiers stack: readonly comes first, then the ? for optionality. Each can have its own + or - operator.
Practical Example
type Config = {
readonly apiKey?: string;
timeout: number;
};
type RequiredAndMutable<T> = {
-readonly [K in keyof T]-?: T[K];
};
type FullConfig = RequiredAndMutable<Config>;
// Result: { apiKey: string; timeout: number }
// Both readonly removed AND made required
This strips both the readonly and optional nature from all properties—perfect for internal validated versions of external configs.
Key Takeaway:
Mapped types can simultaneously manipulate both readonly and ? modifiers using + and - prefixes, enabling complex transformations that control both mutability and optionality in a single type operation.