Skip to content
Utility Types
step 1/5

Reading — step 1 of 5

Learn

~1 min readType-Level Programming

TypeScript ships with a bunch of utility types — generic types that transform other types.

interface User {
    id: number;
    name: string;
    email: string;
    password: string;
}

Partial<T> — every field becomes optional:

type UserUpdate = Partial<User>;
// { id?: number; name?: string; email?: string; password?: string }

Required<T> — opposite, every optional becomes required.

Readonly<T> — every field becomes readonly.

Pick<T, K> — keep only listed keys:

type UserPublic = Pick<User, "id" | "name" | "email">;
// { id: number; name: string; email: string }

Omit<T, K> — remove listed keys:

type UserPublic = Omit<User, "password">;
// same as Pick example above

Record<K, V> — object with given key/value types:

type Status = "active" | "archived" | "pending";
type StatusCounts = Record<Status, number>;
// { active: number; archived: number; pending: number }

ReturnType<F> — the return type of a function:

function getUser() { return { id: 1, name: "Ada" }; }
type User = ReturnType<typeof getUser>;
// { id: number; name: string }

Parameters<F> — tuple of a function's parameter types.

Awaited<T> — unwrap a Promise.

These plus a few more (Exclude, Extract, NonNullable) cover most type manipulation needs.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Utility Types — TypeScript Intermediate