Sub topic 3
Adding and removing readonly and optional modifiers
This chapter assumes you can write object types, mapped types with [K in keyof T], and generic type aliases. You should also know the difference between T[K] (an indexed access type) and keyof T. If any of these are new, review them first.
A mapped type can change two property settings on every key it visits: whether the property is readonly (cannot be reassigned) and whether it is optional (may be missing). You control each setting with a modifier written in front of the mapped type's readonly or ? token.
The four modifiers
| Modifier | Effect on each property | Example |
|---|---|---|
| `readonly` or `+readonly` | Makes the property read-only | `{ readonly [K in keyof T]: T[K] }` |
| `-readonly` | Removes read-only | `{ -readonly [K in keyof T]: T[K] }` |
| `?` or `+?` | Makes the property optional | `{ [K in keyof T]?: T[K] }` |
| `-?` | Makes the property required | `{ [K in keyof T]-?: T[K] }` |
A plus sign is optional. +readonly and readonly mean the same thing.
A worked example
Start with a type that mixes both settings:
type User = {
id: number;
name?: string;
readonly createdAt: Date;
};
type Editable<T> = { -readonly [K in keyof T]: T[K] };
type Draft<T> = { [K in keyof T]?: T[K] };
type Saved<T> = { [K in keyof T]-?: T[K] };
const draft: Draft<User> = { name: "Ada" };
const user: Saved<Editable<User>> = {
id: 1,
name: "Ada",
createdAt: new Date(),
};
user.createdAt = new Date();
console.log(user.name);Editable<User> removes readonly from createdAt. Saved then removes the ? from name, so user must include every property. Draft<User> adds ? to every property, which is why draft can hold only name.
The original User still has a read-only createdAt. Assigning to it through a User value is a compile error.
Homomorphic mappings keep modifiers
A mapped type written as [K in keyof T] is called homomorphic. TypeScript copies the readonly and ? settings from T onto the result. You do not need to repeat them.
A mapping over a fixed union does not copy anything:
type Fixed = { [K in "a" | "b"]: string };
// { a: string; b: string } with no modifiersUse keyof T when you want the source modifiers to survive. Use an explicit union when you want to build a new shape.
> Note: Required<T> and Partial<T> in the standard library are built this way: Required<T> uses -? and Partial<T> uses ?.
Common mistakes
- Writing
readonlyon a property inside an object that you then pass to a function expecting a mutable type. The function accepts it, but the type system stops you from mutating it inside the function. - Expecting
-?to fix a value that is actuallyundefinedat runtime. It only changes the type. - Expecting modifiers to carry over when you map over a union or a computed key list.
Exercise
Exercise
Given type Settings = { theme?: string; readonly version: number };, write a type Locked<T> that makes every property read-only and required. Then state the result for Settings.
Show answer
type Locked<T> = { readonly [K in keyof T]-?: T[K] }; The result for Settings is { readonly theme: string; readonly version: number }.