Common Pitfalls with Modifier Syntax
You'll learn: How to avoid the most common confusion when using modifier syntax in mapped types—mistaking what -readonly means versus what no modifier means.
The Subtle but Critical Difference
When working with mapped types, there are three distinct states for modifiers:
+readonly(or justreadonly) — adds readonly to properties-readonly— removes readonly from properties- No modifier at all — preserves whatever the original property had
The most common mistake beginners make is thinking that leaving off a modifier means removal. It doesn't. Omitting readonly means preservation, not removal.
The Confusion
type User = {
readonly id: string;
name: string;
};
// WRONG ASSUMPTION: This removes readonly
type MutableUser = {
[K in keyof User]: User[K];
};
// id is STILL readonly! You preserved the original modifiers.
// CORRECT: This actually removes readonly
type TrulyMutableUser = {
-readonly [K in keyof User]: User[K];
};
// Now id is mutable
Without the -readonly, you're just copying properties as-is. You must explicitly use the minus to strip modifiers.
The Same Rule for Optional
type Partial = { name?: string };
// Preserves optional (doesn't make it required)
type StillPartial = { [K in keyof Partial]: Partial[K] };
// Actually makes it required
type MadeRequired = { -readonly [K in keyof Partial]-?: Partial[K] };
Key Takeaway: The minus sign (-) actively removes a modifier; omitting the modifier keyword entirely preserves the original state. Don't confuse absence with removal.