Skip to the content

Lesson 111 of 296

Utility types

What you will learn

  • The built-in utility types and when to use each
  • Practical examples from real apps
On this page

What are utility types?

TypeScript includes ready-made generic types that transform other types. They save you from writing similar types again and again.

Using this example type:

type User = {
  id: number;
  name: string;
  email: string;
  password: string;
  role?: "admin" | "user";
};

Partial and Required

type UserUpdate = Partial<User>;

Partial makes every property optional. Perfect for "update" functions where only some fields change:

function updateUser(id: number, changes: Partial<User>) { ... }
updateUser(1, { name: "Samuel" });

Required does the opposite: makes every property required, including role.

Readonly

const frozen: Readonly<User> = getUser();
frozen.name = "x";        Error

Makes every property readonly (shallow, like Object.freeze, but only at compile time).

Pick and Omit

Pick keeps only the listed properties:

type UserPreview = Pick<User, "id" | "name">;

Omit removes the listed properties:

type PublicUser = Omit<User, "password">;
type NewUser = Omit<User, "id">;          the database creates the id

Omit is excellent for "never send the password to the front end" (Lesson 62).

Record

type Role = "admin" | "editor" | "viewer";
const permissions: Record<Role, string[]> = {
  admin: ["read", "write", "delete"],
  editor: ["read", "write"],
  viewer: ["read"]
};

Record<K, V>: an object type with keys K and values V.

Exclude, Extract and NonNullable

These work on union types:

type Status = "pending" | "paid" | "shipped" | "cancelled";

type ActiveStatus = Exclude<Status, "cancelled">;           "pending" | "paid" | "shipped"
type Done = Extract<Status, "shipped" | "cancelled">;       "shipped" | "cancelled"
type Name = NonNullable<string | null | undefined>;         string

ReturnType and Parameters

Get types from functions:

function createUser(name: string, email: string) {
  return { id: crypto.randomUUID(), name, email, createdAt: new Date() };
}

type CreatedUser = ReturnType<typeof createUser>;
type CreateUserArgs = Parameters<typeof createUser>;       [name: string, email: string]

Useful when a library does not export the types you need.

Awaited

Gets the value type inside a promise:

type A = Awaited<Promise<string>>;                         string
type U = Awaited<ReturnType<typeof fetchUser>>;            the user type, not the promise

Summary

UtilityDoes
Partial<T>All properties optional
Required<T>All properties required
Readonly<T>All properties readonly
Pick<T, K>Keep only keys K
Omit<T, K>Remove keys K
Record<K, V>Object with keys K and values V
Exclude<U, X>Remove X from union U
Extract<U, X>Keep only the parts of U that fit X
NonNullable<T>Remove null and undefined
ReturnType<F>The return type of a function type
Parameters<F>The parameter types, as a tuple
Awaited<T>The value inside a promise

Also useful: Uppercase, Lowercase, Capitalize, Uncapitalize for string literal types (Lesson 112).

Combining them

type UserForm = Partial<Omit<User, "id" | "password">>;

Common mistakes

  • Duplicating a type by hand instead of using Pick or Omit, so the two drift apart.
  • Using Partial for data that must be complete.
  • Forgetting that Readonly is shallow and compile-time only.

How to say it in an interview

"Utility types transform existing types so I do not duplicate them. Partial for update payloads, Pick and Omit to create views such as a public user without the password, Record for dictionaries keyed by a union, Exclude, Extract and NonNullable for unions, and ReturnType, Parameters and Awaited to derive types from functions and promises. Deriving types keeps them in sync with the source of truth."

Key words

WordMeaning
Utility typeA built-in generic type that transforms another type
Derived typeA type built from another type instead of written by hand

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.