← Back to libraryQuestion 224 of 468
🔷TypeScriptAdvanced

Utility Types (Partial, Pick, Omit, Record)

📌 Definition:

TypeScript ships built-in utility types that transform existing types: Partial<T> (all optional), Required<T>, Readonly<T>, Pick<T, K> (subset of keys), Omit<T, K> (all but keys), and Record<K, V> (map type).

📖 Detailed Explanation:

These derive new types from existing ones without duplication. Partial<User> makes every field optional (great for update payloads/patches); Pick<User, 'id' | 'name'> selects a subset; Omit<User, 'password'> removes fields (e.g. a safe DTO); Record<string, number> types an object map; Required/Readonly flip modifiers. There are more (ReturnType, Parameters, Awaited, NonNullable) built on generics and mapped types. Using utility types keeps a single source of truth: change the base type and all derived types update. Testers use them to type request/response variants and fixtures precisely.

🔑 Key Points:
  • Partial<T> all-optional; Required<T> all-required
  • Pick<T,K> subset; Omit<T,K> remove keys
  • Record<K,V> typed object map
  • Derive from one base type → single source of truth
🌍 Real-World Example:

Typing a PATCH request: function updateUser(id: number, changes: Partial<User>) accepts any subset of user fields, so the type stays in sync with User automatically as fields are added.

🎯 Scenario-Based Interview Question:

You need a type for an update payload that allows ANY subset of a User's fields. Which utility type and why?