AdvancedTypeScript
Utility Types
Learn TypeScript’s built-in utility types — Partial, Required, Pick, Omit, Record, and more — for transforming existing types.
Reading Time
18 min
Lesson
Lesson 22 of 30
Why Utility Types?
TypeScript ships with a set of built-in generic types that transform an existing type into a new one, so you don’t need to redefine similar shapes by hand.
Partial<T> and Required<T>
interface User {
name: string;
email: string;
age?: number;
}
// All properties become optional — perfect for a partial update function
type UserUpdate = Partial<User>;
function updateUser(id: number, changes: UserUpdate) { /* ... */ }
updateUser(1, { name: "New Name" }); // OK, other fields omitted
// All properties become required, even ones that were optional
type CompleteUser = Required<User>;Pick<T, K> and Omit<T, K>
// Pick: keep only the listed properties
type UserPreview = Pick<User, "name" | "email">;
// Omit: keep everything except the listed properties
type UserWithoutEmail = Omit<User, "email">;Record<K, V>
// A dictionary type: keys of type K, values of type V
type RolePermissions = Record<"admin" | "editor" | "viewer", boolean>;
const permissions: RolePermissions = {
admin: true,
editor: true,
viewer: false,
};Common Utility Types
| Utility Type | What It Does |
|---|---|
| Partial<T> | Makes every property in T optional |
| Required<T> | Makes every property in T required |
| Readonly<T> | Makes every property in T readonly |
| Pick<T, K> | Keeps only the listed keys K from T |
| Omit<T, K> | Removes the listed keys K from T |
| Record<K, V> | Builds an object type with keys K and values V |
| ReturnType<F> | Extracts a function’s return type |
| Parameters<F> | Extracts a function’s parameter types as a tuple |
ReturnType<F> and Parameters<F>
function createUser(name: string, age: number) {
return { name, age, createdAt: new Date() };
}
type NewUser = ReturnType<typeof createUser>;
// { name: string; age: number; createdAt: Date }
type CreateUserArgs = Parameters<typeof createUser>;
// [name: string, age: number]Utility Types Are Built from Simpler Features
These utilities aren’t magic — they’re implemented internally using mapped types and conditional types, the two features covered in the next couple of lessons.
Best Practice
Reach for a utility type before writing a near-duplicate interface by hand — Partial, Pick, and Omit alone cover a huge share of everyday type transformations.