CPCODELAB
TypeScript

Built-in Utility Types

12 min read

Built-in Utility Types

TypeScript ships with a set of generic utility types that transform existing types. They are implemented using mapped types and conditional types internally, but you can use them without knowing how they work.

ts
interface User {
  id: number;
  name: string;
  email: string;
  password: string;
}

// Partial — all properties optional
type UserUpdate = Partial<User>;
// { id?: number; name?: string; email?: string; password?: string }

// Required — all properties required (reverses Partial)
type StrictUser = Required<UserUpdate>;

// Readonly — no property can be reassigned
type ImmutableUser = Readonly<User>;

// Pick — keep only selected properties
type PublicUser = Pick<User, "id" | "name">;
// { id: number; name: string }

// Omit — remove selected properties
type SafeUser = Omit<User, "password">;
// { id: number; name: string; email: string }
ts
// Record — map keys to a value type
type RolePermissions = Record<"admin" | "student" | "mentor", string[]>;
const perms: RolePermissions = {
  admin: ["read", "write", "delete"],
  student: ["read"],
  mentor: ["read", "write"]
};

// ReturnType — extract a function's return type
function createSession() {
  return { token: "abc", expiresAt: new Date() };
}
type Session = ReturnType<typeof createSession>;
// { token: string; expiresAt: Date }

// Parameters — extract parameter types as a tuple
type AddParams = Parameters<(a: number, b: number) => number>;
// [a: number, b: number]

// Awaited — unwrap Promise types (TypeScript 4.5+)
type Resolved = Awaited<Promise<string>>; // string

Other useful built-in types include NonNullable<T>, Extract<T, U>, Exclude<T, U>, InstanceType<T>, and ConstructorParameters<T>. Browse the TypeScript Handbook's utility types page for the full list.

Ready to test yourself?
Practice TypeScript— quiz & coding exercises