Built-in Utility Types

TypeScript provides globally available utility types to transform existing types easily without rewriting code.

1. Object Transformation Utilities

Utility TypeDescriptionCode Syntax Example
Partial<T>Makes all properties in T optionaltype DraftUser = Partial<User>;
Required<T>Makes all properties in T requiredtype StrictUser = Required<User>;
Readonly<T>Makes all properties in T immutabletype ImmutableUser = Readonly<User>;
Pick<T, K>Selects specific keys K from Ttype UserPreview = Pick<User, "name" | "email">;
Omit<T, K>Removes specific keys K from Ttype UserWithoutPassword = Omit<User, "password">;
Record<K, T>Constructs an object with key K and value Ttype UserMap = Record<string, User>;

2. Live Code Example: `Partial` & `Omit`

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

// 1. Partial: Useful for Update API payloads
function updateUser(id: number, fieldsToUpdate: Partial<User>) {
  // fieldsToUpdate allows optional { name?, email?, role? }
}

updateUser(101, { email: "newemail@example.com" });

// 2. Omit: Useful for Create User DTOs (where id is auto-generated)
type CreateUserDTO = Omit<User, "id">;

const newUser: CreateUserDTO = {
  name: "Bob",
  email: "bob@example.com",
  role: "User"
};

3. Union Filtering (`Extract` & `Exclude`)

type Event = "click" | "hover" | "scroll" | "mousemove";

// Exclude "mousemove" from Event union
type NonMouseEvents = Exclude<Event, "mousemove">; // "click" | "hover" | "scroll"

// Extract only "click" | "hover"
type InteractiveEvents = Extract<Event, "click" | "hover">;

Next Up

Explore advanced Mapped Types (`[K in keyof T]`) and Conditional Types (`infer`).

Next Lesson: Mapped & Conditional Types →