Lesson 22 / 26
Utility Types at a Glance
Built-in helpers that transform an existing type: Partial, Pick, Omit, Record, Readonly.
Partial & Pick
Partial<T> makes every property optional (great for update payloads). Pick<T, K> keeps only the listed keys.
interface User {
id: number;
name: string;
email: string;
}
type UserUpdate = Partial<User>; // all fields optional
type UserPreview = Pick<User, "id" | "name">; // { id; name }Omit & Record
Omit<T, K> keeps everything except the listed keys. Record<K, V> builds an object type mapping every key in K to type V.
type UserNoEmail = Omit<User, "email">; // { id; name }
type Roles = "admin" | "editor" | "viewer";
type Permissions = Record<Roles, boolean>;
// { admin: boolean; editor: boolean; viewer: boolean }Readonly<T>
Readonly<T> marks every property readonly, freezing the type without touching the original.
type FrozenUser = Readonly<User>;
const u: FrozenUser = { id: 1, name: "Ada", email: "a@x.com" };
u.name = "Bo";
// Error: Cannot assign to 'name' because it is a read-only property.Compose them
Utility types nest: Readonly<Partial<Pick<User, "name" | "email">>> builds exactly the shape you need without a new interface.