Sub topic 6
Composing Utility Types
Assume you know TypeScript interfaces, type aliases, union types, string literal types, and generic syntax. This chapter focuses on combining utility types to describe related data shapes without repeating property lists.
Build types from one source
A single domain type often supports several operations. A User type may describe data stored in a database, while an API needs different shapes for creating, updating, and displaying users.
interface User {
id: number;
name: string;
email: string;
isAdmin: boolean;
}
type UserPreview = Pick<User, "id" | "name">;
type NewUser = Omit<User, "id">;
type UserUpdate = Partial<Omit<User, "id">>;
function createUser(input: NewUser): User {
return {
id: 1,
...input,
};
}
function updateUser(id: number, changes: UserUpdate): void {
console.log(`Updating user ${id}`, changes);
}
const preview: UserPreview = {
id: 1,
name: "Ada",
};
const created = createUser({
name: "Grace",
email: "[email protected]",
isAdmin: false,
});
updateUser(created.id, { email: "[email protected]" });
console.log(preview, created);Pick<User, "id" | "name"> keeps only the selected properties. Omit<User, "id"> removes id. Wrapping that result in Partial makes every remaining property optional.
This gives each operation a useful rule:
| Operation | Type expression | Meaning | Column |
|---|---|---|---|
| Preview | `Pick<User, "id" \ | "name">` | Return only selected fields |
| Create | `Omit<User, "id">` | Let the server assign the identifier | |
| Update | `Partial<Omit<User, "id">>` | Allow any subset of editable fields |
Read nested utility types
Utility types are generic types, so one utility can receive the result of another. Read the expression from the inside out:
- Start with
User. - Apply
Omit<User, "id">. - Apply
Partial<...>to the result.
The parentheses are usually unnecessary because the angle brackets show the nesting:
type EditableUser = Omit<User, "id">;
type UserUpdate = Partial<EditableUser>;Naming an intermediate type can make complex code easier to inspect. Keeping the expression inline is useful when the type is short and used only once.
A composition can also include Readonly:
type ReadonlyPreview = Readonly<Pick<User, "id" | "name">>;
const result: ReadonlyPreview = {
id: 1,
name: "Ada",
};
// result.name = "Grace"; // Compile-time errorReadonly prevents reassignment through that type. It does not automatically freeze the runtime object. JavaScript can still mutate the object through another reference unless you use a runtime mechanism such as Object.freeze.
Choose composition deliberately
Composition works best when the source type represents stable domain data and the derived type represents a clear boundary, such as an API request or response.
Watch for these common mistakes:
- Applying
Partial<User>to a create request can make required fields optional. - Forgetting
Omit<User, "id">can let callers provide an identifier that the server should control. - Using
Readonlyas if it were deep immutability does not protect nested objects. - Repeating property names instead of deriving them can cause types to become inconsistent when
Userchanges.
Exercise
Define a type named UserUpdate for an interface User that has id, name, email, and isAdmin properties. The update must allow any subset of properties except id.
Show answer
Use type UserUpdate = Partial<Omit<User, "id">>;. Omit removes id, and Partial makes the remaining properties optional.