On this page

Utility Types

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

TypeScript ships with a rich set of built‑in utility types.

They’re all defined using generics and mapped types, but you don’t need to understand their internals to use them effectively.

We’ll cover the most common and useful ones.


Partial<T>​

Makes all properties optional.

Example​


interface User {

  id: number;

  name: string;

  email: string;

}



type UserUpdate = Partial<User>;

Equivalent to:


{

  id?: number

  name?: string

  email?: string

}

When to use it​


Required<T>​

Makes all properties required (the opposite of Partial).


type CompleteUser = Required<User>;

Useful when you want to enforce completeness after validation.


Readonly<T>​

Makes all properties immutable.


type ImmutableUser = Readonly<User>;

Great for:


Pick<T, K>​

Selects a subset of properties.


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

Equivalent to:


{

  id: number;

  name: string;

}

When to use it​


Omit<T, K>​

The opposite of Pick: removes properties.


type UserWithoutEmail = Omit<User, "email">;

When to use it​


Record<K, T>​

Creates an object type with keys of type K and values of type T.


type ScoreMap = Record<string, number>;

Equivalent to:


{

  [key: string]: number

}

When to use it​


ReturnType<T>​

Extracts the return type of a function.


function createUser() {

  return { id: 1, name: "Alice" };

}



type User = ReturnType<typeof createUser>;

Now User is:


{

  id: number;

  name: string;

}

When to use it​


Parameters<T>​

Extracts a tuple of a function’s parameter types.


function log(x: string, y: number) {}



type Args = Parameters<typeof log>;

// [string, number]

Useful for:


ConstructorParameters<T>​

Same as Parameters, but for class constructors.


class Point {

  constructor(

    public x: number,

    public y: number,

  ) {}

}



type PointArgs = ConstructorParameters<typeof Point>;

// [number, number]

InstanceType<T>​

Gets the instance type of a class.


type PointInstance = InstanceType<typeof Point>;

Equivalent to:


{

  x: number;

  y: number;

}

NonNullable<T>​

Removes null and undefined.


type Clean = NonNullable<string | null | undefined>;

// string

Great for narrowing and safe APIs.


Exclude<T, U>​

Removes types from a union.


type Status = "loading" | "success" | "error";



type WithoutError = Exclude<Status, "error">;

// "loading" | "success"

Extract<T, U>​

Keeps only the overlapping types.


type OnlyError = Extract<Status, "error">;

// "error"

When to Use Which Utility Type​

Here’s a quick guide:

Utility TypeUse When You Need To…PartialMake everything optional (updates, forms)RequiredEnforce completeness after validationReadonlyPrevent mutation (state, config)PickSelect a subset of fieldsOmitRemove fields (sensitive or computed)RecordCreate dictionaries or mapsReturnTypeDerive types from functionsParametersWrap or transform functionsInstanceTypeGet the type of a class instanceExcludeRemove union membersExtractKeep only specific union membersNonNullableRemove null/undefined

This table alone will save you hours of typing and refactoring.


Putting It All Together​

Here’s a realistic example using multiple utility types:


interface User {

  id: string;

  name: string;

  email: string;

  password: string;

}



// API response should not expose password

type PublicUser = Omit<User, "password">;



// Update payload should allow partial fields

type UserUpdate = Partial<PublicUser>;



// Map of users by ID

type UserMap = Record<string, PublicUser>;



// Factory return type

function createUser(): PublicUser {

  return { id: "1", name: "Alice", email: "[email protected]" };

}



type CreatedUser = ReturnType<typeof createUser>;

This is exactly how real TypeScript applications model data safely and flexibly.


Summary​

In this lesson, you learned the most important TypeScript utility types and when to use them:

1. Transforming object shapes​

2. Creating flexible data structures​

3. Deriving types from functions and classes​

4. Working with unions​

Utility types are the backbone of modern TypeScript—they let you build powerful abstractions with minimal code.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Generic Interfaces & Classes

Next

Hands‑on: Build a Generic Data‑Fetching Wrapper with Typed Responses