TypeScript1 min read

Mastering TypeScript Utility Types: Partial, Required, Readonly, Record, Pick, Omit & Exclude

Comprehensive guide to TypeScript built-in utility types and implementing custom dynamic types from scratch in live coding interviews.

Pairlet Team•Published: •Updated:

TypeScript includes powerful built-in Utility Types that facilitate global type transformations. Mastering these types enables engineers to write DRY, robust type definitions.

Core Built-in Utility Types

  • •Partial<T>: Makes all properties optional.
  • •Required<T>: Makes all properties required.
  • •Readonly<T>: Makes all properties immutable.
  • •Record<K, T>: Constructs an object type with key type K and value type T.
  • •Pick<T, K>: Constructs a type by picking specific properties K from T.
  • •Omit<T, K>: Constructs a type by removing properties K from T.
  • •Exclude<T, U>: Removes union members that are assignable to U.
  • •ReturnType<T>: Extracts the return type of a function type.
TYPESCRIPT
interface UserProfile {
  id: string;
  username: string;
  email: string;
  bio?: string;
}

// Pick only public fields
type PublicUser = Pick<UserProfile, "id" | "username">;

// Omit sensitive email field
type SafeUser = Omit<UserProfile, "email">;

// Make profile fields optional for update operations
type UpdateUserProfileInput = Partial<Omit<UserProfile, "id">>;
TYPESCRIPT
type PublicStatus = "active" | "invited" | "suspended";
type VisibleStatus = Exclude<PublicStatus, "suspended">;

async function loadUser(id: string) {
  const response = await fetch(`/users/${id}`);
  return response.json() as Promise<UserProfile>;
}

type UserLoaderResult = ReturnType<typeof loadUser>;

Custom Mapped Types (Interview Question)

Implement custom MyOmit<T, K> without using the built-in Omit:

TYPESCRIPT
type MyOmit<T, K extends keyof T> = {
  [P in keyof T as P extends K ? never : P]: T[P];
};

Practice TypeScript Coding Live

Test TypeScript generic utilities live in a shared editor. Create a Pairlet Room.
Practice Relevant Coding Problems
Practice Live Coding

Conduct Live Coding Interviews in the Browser

No candidate sign-up required. Create a room, share the link, and code together in real time with shared code execution.

Related Articles