Tech Interview RoomStudy guides

TypeScript

Which built-in utility types do you use most?

Intermediate · 2 min read · Lesson 5 of 7 in TypeScript

Utility types transform existing types so you do not repeat yourself.

  • Partial<T>: all properties optional (patch payloads, form drafts).
  • Required<T>: all properties required.
  • Readonly<T>: all properties readonly (props, frozen config).
  • Pick<T, 'a' | 'b'> and Omit<T, 'password'>: choose or drop properties (public DTOs, wrapper props).
  • Record<K, V>: an object with keys K and values V (lookup tables).
  • Exclude<U, X>, Extract<U, X>: filter members of a union.
  • NonNullable<T>: remove null and undefined.
  • ReturnType<typeof fn>, Parameters<typeof fn>: types derived from functions.
  • Awaited<T>: the resolved type of a promise.
  • React: React.ComponentProps<typeof Button> or ComponentProps<'button'> to reuse another component's or element's props.

Why it matters

Deriving types keeps them in sync: when User gets a new field, Partial<User> and Omit<User, 'password'> update automatically.

Example

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

type PublicUser = Omit<User, 'password'>;
type UserPatch = Partial<Pick<User, 'name' | 'email'>>;
type Role = 'admin' | 'editor' | 'viewer';
const labels: Record<Role, string> = { admin: 'Admin', editor: 'Editor', viewer: 'Viewer' };

async function loadUser() { return { id: '1', name: 'A' }; }
type Loaded = Awaited<ReturnType<typeof loadUser>>;   // { id: string; name: string }

type NativeButton = React.ComponentProps<'button'>;
type MyButtonProps = NativeButton & { variant: 'primary' | 'ghost' };

Follow-up questions interviewers ask

Try answering each one before you open it.

How is Omit implemented?

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; It takes all keys of T, removes K with Exclude, and picks what is left. Note that Omit on a union type is not distributive; write a distributive version with a conditional type if you need one per variant.