Back

TypeScript Partial Utility Type

`Partial< T >` makes all properties of an object optional. It is especially useful when working with incomplete data or partial updates.

What Does Partial Do?

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

With `Partial`, you can make every property of a type optional without marking each one individually with `?`.

Practical Use: Update Functions

function updateUser(id: number, update: Partial<User>) {
    console.log(`Updating user ${id}:`, update);
  }
  
  updateUser(1, { name: "Alice" });
  // Output: Updating user 1: { name: 'Alice' }
  
  updateUser(2, { email: "bob@example.com" });
  // Output: Updating user 2: { email: 'bob@example.com' }

Using `Partial< T >` simplifies update operations where not all fields are required.

Working with Incomplete Data

const draft: Partial<User> = {
    email: "test@example.com"
  };
  
  console.log(draft);
  // Output: { email: 'test@example.com' }

You can safely create objects even when some data is missing using `Partial`.

When to Use Partial?

`Partial< T >` is ideal for scenarios like form inputs, optional settings, and PATCH API requests.

Back