Sub topic 10
Changing Modifiers in Bulk: `Partial`, `Required`, `Readonly`, `-?`, and `-readonly`
Assume you know TypeScript interfaces, type aliases, and union types such as string | undefined. This chapter focuses on mapped types: types that transform every property of another type.
Why transform object modifiers?
Suppose an application stores a complete profile:
interface Profile {
readonly id: number;
name: string;
bio?: string;
}Different operations need different versions of this shape:
- A form update may allow any subset of properties.
- A response object may need every property.
- A configuration passed into a function may be read-only.
- An editor may need to remove
readonlybefore changing a value.
TypeScript provides utility types and mapped-type modifiers for these transformations.
The built-in utility types
Partial<T> makes every property optional. Required<T> makes every property required. Readonly<T> makes every property read-only.
interface Profile {
readonly id: number;
name: string;
bio?: string;
}
type ProfileUpdate = Partial<Profile>;
type CompleteProfile = Required<Profile>;
type ViewProfile = Readonly<Profile>;
const update: ProfileUpdate = { bio: "TypeScript developer" };
const complete: CompleteProfile = {
id: 1,
name: "Ada",
bio: "Mathematician"
};
const view: ViewProfile = complete;
// view.name = "Grace"; // Error: name is read-onlyPartial<Profile> changes id, name, and bio into optional properties. It does not change their value types. Readonly<Profile> prevents assignments through that type, but it does not freeze the object at runtime.
Required<Profile> removes optionality from bio. The value must be present when creating a CompleteProfile.
| Utility type | Effect on each property |
|---|---|
| `Partial<T>` | Adds `?` |
| `Required<T>` | Removes `?` |
| `Readonly<T>` | Adds `readonly` |
Writing mapped-type modifiers
The utility types use mapped types. A mapped type iterates over the keys of an existing type:
type Optional<T> = {
[K in keyof T]?: T[K];
};
type ReadOnly<T> = {
readonly [K in keyof T]: T[K];
};Here, K in keyof T visits each property key. T[K] retrieves that property's value type.
A modifier can be removed by placing a minus sign before it:
type Filled<T> = {
[K in keyof T]-?: T[K];
};
type Mutable<T> = {
-readonly [K in keyof T]: T[K];
};-? removes optionality. -readonly removes the read-only modifier.
These types transform Profile as follows:
type EditableProfile = Mutable<Profile>;
// { id: number; name: string; bio?: string }
type FullProfile = Filled<Profile>;
// { readonly id: number; name: string; bio: string }Notice that Filled<Profile> removes ? from bio but keeps readonly on id. Each modifier is controlled independently.
Combining modifiers
You can add or remove several modifiers in one mapped type:
type EditableComplete<T> = {
-readonly [K in keyof T]-?: T[K];
};
type Draft<T> = {
-readonly [K in keyof T]?: T[K];
};
interface Settings {
readonly theme: string;
readonly language: string;
}
type SettingsDraft = Draft<Settings>;
type FinalSettings = EditableComplete<Settings>;
const draft: SettingsDraft = { theme: "dark" };
draft.language = "en";
const finalSettings: FinalSettings = {
theme: "dark",
language: "en"
};
finalSettings.theme = "light";Use these transformations to describe a real stage of your program. A draft may be incomplete and mutable. A validated result may be complete. A value shared with consumers may be read-only.
How the transformations relate
Common errors
Partial<T> does not mean every value is undefined. It means the property may be absent. Reading a property from a partial value still produces a possibly undefined result.
Required<T> does not validate data at runtime. An object received from JSON can still be missing fields. Runtime validation is needed at a data boundary.
-readonly does not make a frozen JavaScript object mutable. It only changes the static type. The runtime object may still have restrictions created by Object.freeze.
Exercise
What type makes every property of Profile required and writable?
Show answer
Define a mapped type with both modifiers removed: type CompleteMutable<T> = { -readonly [K in keyof T]-?: T[K] }.