Making All Properties Mutable
What you'll learn: How to use the -readonly modifier to strip readonly protection from all properties in a type, creating a fully mutable version.
The Core Idea
You've already learned how to add readonly to every property in a type using mapped types. But sometimes you need to go the opposite direction: take a type where properties are readonly and make them all writable again.
TypeScript lets you remove modifiers just as you can add them. The -readonly syntax in a mapped type strips away any readonly constraints, giving you back full mutability.
The Syntax
type Mutable<T> = {
-readonly [K in keyof T]: T[K];
};
The minus sign (-) before readonly tells TypeScript: "Remove the readonly modifier if it exists."
Seeing It in Action
type LockedConfig = {
readonly apiKey: string;
readonly maxRetries: number;
};
type EditableConfig = Mutable<LockedConfig>;
// Result:
// {
// apiKey: string;
// maxRetries: number;
// }
const config: EditableConfig = {
apiKey: "abc123",
maxRetries: 3
};
config.apiKey = "new-key"; // ✓ Allowed now
When This Matters
This pattern is useful when you receive a readonly type from a library or API, but you need to work with a mutable version internally—perhaps for building up an object incrementally before freezing it again.
Key Takeaway: The -readonly modifier in mapped types removes readonly constraints from all properties, restoring full mutability at compile time.