Utility Types

View saved

Partial for optional updates

Partial<T> makes every property optional—handy for patch objects.

interface User {
  name: string;
  email: string;
}

function update(user: User, patch: Partial<User>): User {
  return { ...user, ...patch };
}

console.log(update({ name: "Ada", email: "[email protected]" }, { email: "[email protected]" }));

Pick and Omit

Select or remove fields without rewriting the whole interface.

type UserName = Pick<User, "name">;
type UserWithoutEmail = Omit<User, "email">;

Readonly

Prevent accidental mutation of properties at the type level.

const settings: Readonly<User> = { name: "Ada", email: "[email protected]" };
// settings.name = "Lin"; // Error

Learn a few well

  • Start with Partial, Pick, Omit, and Readonly
  • Record<K, V> maps keys to a value type
  • Utility types are compile-time only
  • Compose them carefully; overly clever types are hard to read

Comments

One comment per signed-in account. Comments are saved with this page’s URL.