TypeScript Utility Types
TypeScript ships with a set of generic utility types that build new types from existing ones. Instead of writing a second interface that is "the same as User, but every field optional", you write Partial<User>. When User changes, the derived type changes with it automatically.
All examples on this page use this interface:
interface User {
id: number;
email: string;
nickname?: string;
role: "admin" | "member";
}Overview
| Utility | Produces |
|---|---|
Partial<T> |
all properties optional |
Required<T> |
all properties required |
Readonly<T> |
all properties read-only |
Pick<T, K> |
only the keys K |
Omit<T, K> |
everything except the keys K |
Record<K, V> |
an object type with keys K and values V |
Exclude<U, X> |
union U without the members assignable to X |
Extract<U, X> |
only the members of U assignable to X |
NonNullable<T> |
T without null and undefined |
ReturnType<F> |
the return type of function type F |
Parameters<F> |
the parameter types of F as a tuple |
Awaited<T> |
the value a promise resolves to |
Partial and Required
Partial is perfect for update functions where the caller passes only the fields that change:
interface User {
id: number;
email: string;
nickname?: string;
role: "admin" | "member";
}
function updateUser(user: User, changes: Partial<User>): User {
return { ...user, ...changes };
}
const u: User = { id: 1, email: "an@example.com", role: "member" };
console.log(updateUser(u, { role: "admin" }));
const complete: Required<User> = { id: 2, email: "bo@example.com", role: "member" };main.ts(15,7): error TS2741: Property 'nickname' is missing in type '{ id: number; email: string; role: "member"; }' but required in type 'Required<User>'.
Required<User> turned the optional nickname into a required one, so the last object is rejected. Delete that line and the first console.log prints the updated user.
Pick and Omit
Use these to make a smaller view of a type — for example, the data a public profile is allowed to show, or the input for creating a user before the database assigns an id:
interface User {
id: number;
email: string;
nickname?: string;
role: "admin" | "member";
}
type PublicProfile = Pick<User, "id" | "nickname">;
type NewUser = Omit<User, "id">;
const profile: PublicProfile = { id: 5, nickname: "kiwi" };
const draft: NewUser = { email: "kiwi@example.com", role: "member" };
console.log(profile);
console.log(draft);{ id: 5, nickname: 'kiwi' }
{ email: 'kiwi@example.com', role: 'member' }
Record
Record<K, V> describes an object used as a dictionary. With a union of literal keys, TypeScript also checks that every key is present:
type Role = "admin" | "member" | "guest";
const permissions: Record<Role, string[]> = {
admin: ["read", "write", "delete"],
member: ["read", "write"],
guest: ["read"],
};
const wordCount: Record<string, number> = {};
for (const w of "to be or not to be".split(" ")) {
wordCount[w] = (wordCount[w] ?? 0) + 1;
}
console.log(permissions.member);
console.log(wordCount);[ 'read', 'write' ]
{ to: 2, be: 2, or: 1, not: 1 }
Working with unions and functions
Exclude, Extract and NonNullable filter unions; ReturnType and Parameters pull types out of functions, which is handy when a library exports a function but not its types.
type Status = "draft" | "published" | "archived" | null;
type Visible = Exclude<Status, "archived" | null>; // "draft" | "published"
type Present = NonNullable<Status>; // no null
function createOrder(product: string, qty: number) {
return { product, qty, total: qty * 9.5 };
}
type Order = ReturnType<typeof createOrder>;
type OrderArgs = Parameters<typeof createOrder>; // [product: string, qty: number]
const v: Visible = "draft";
const args: OrderArgs = ["pen", 4];
const order: Order = createOrder(...args);
console.log(v, args, order.total);draft [ 'pen', 4 ] 38
Tip: Utility types compose.
Readonly<Partial<User>>is a read-only object where every field is optional. In the Mapped Types lesson you'll see howPartialand friends are implemented — and how to write your own.
Test yourself
Which utility type makes every property optional?
- Optional<T>
- Partial<T>
- Required<T>
- Pick<T, K>
What is Omit<User, "id">?
- Only the
idproperty - User with
idmade optional - User with every property except
id - An error
Given function f() { return 42; }, what is ReturnType<typeof f>?
typeof f- number
() => number- 42