Browse topics

TypeScript utility types

Version
es2025
Review
Draft

Summary#

Utility types derive new types from existing ones. Partial makes properties optional, Pick selects them, Omit removes them and Record maps a set of keys to one value type.

Syntax#

ts
type Changes = Partial<Model>;
type Preview = Pick<Model, "id" | "name">;
type Draft = Omit<Model, "id">;
type Lookup = Record<Key, Value>;

Example#

ts
interface User {
  id: number;
  name: string;
  admin: boolean;
}

const draft: Omit<User, "id"> = { name: "Ada", admin: false };
const changes: Partial<User> = { admin: true };
const user: User = { id: 1, ...draft, ...changes };
const label: Pick<User, "id" | "name"> = user;
const roles: Record<"admin" | "guest", number> = { admin: 1, guest: 0 };

console.log(label.name, user.admin, roles.admin);
console.log(Object.keys(label).join(","));

Expected output#

Text
Ada true 1
id,name,admin

Common mistakes#

These types change only compile-time checking. Assigning to a Pick type does not strip other properties from the object. Partial affects the top level only, and Omit accepts a misspelled key without an error.

Version applicability#

Partial, Pick and Record arrived in TypeScript 2.1. Omit arrived in TypeScript 3.5, which the complete example requires. TypeScript 2.1 added support for the object spread used in the example. All four types are erased from the emitted JavaScript.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0