What are Utility Types in TypeScript?
Learn TypeScript utility types like Partial, Pick, Omit, and Record with clear examples, interview tips, and code to reshape types without duplication.
Expected Interview Answer
Utility types are built-in generic types that transform an existing type into a new one, so you can reshape types without redefining them by hand. Common examples are Partial, Required, Readonly, Pick, Omit, Record, Exclude, Extract, and ReturnType.
Each utility type takes one or more type arguments and produces a derived type using mapped and conditional type machinery under the hood. Partial<T> makes every property optional, Pick<T, K> keeps only the named keys, Omit<T, K> drops them, and Record<K, V> builds an object type from a set of keys. Because they derive from a source type, updating the original automatically flows through to everything built on top of it.
- Removes duplicate type definitions
- Keeps derived types in sync with their source
- Expresses intent clearly (Partial, Readonly, Pick)
- Reduces boilerplate for updates and DTOs
- Ships with the compiler — no libraries needed
AI Mentor Explanation
Think of a full team sheet listing every player's name, role, and batting order. Utility types are like standard team forms derived from it: a 'probable XI' form makes every slot optional, a 'declared XI' locks the sheet read-only, and a 'batting-only card' picks just the batters. One master sheet, many official derived forms without rewriting player details.
Step-by-Step Explanation
Step 1
Start from a source type
Define a base interface or type such as User that holds the canonical shape.
Step 2
Choose the transformation
Decide whether you need optional (Partial), locked (Readonly), a subset (Pick), or a removal (Omit).
Step 3
Apply the utility
Wrap the source type, e.g. Partial<User> or Pick<User, 'id' | 'name'>, to derive the new type.
Step 4
Use the derived type
Annotate function params, DTOs, or state with the derived type instead of a hand-written duplicate.
Step 5
Let changes propagate
Edit only the source type; every utility-derived type updates automatically at compile time.
What Interviewer Expects
- Names several utility types and what each does
- Understands they are generic and derive from a source
- Knows the Pick vs Omit distinction
- Can explain Record<K, V> for key/value maps
- Mentions they reduce duplication and drift
Common Mistakes
- Confusing Pick and Omit or their argument order
- Thinking Partial makes a type nullable rather than optional
- Assuming Readonly is deep rather than shallow
- Reinventing utility behavior manually instead of composing built-ins
- Believing utility types add runtime code (they are erased)
Best Answer (HR Friendly)
“Utility types are ready-made tools in TypeScript that create a new type from an existing one, like making all fields optional or picking just a few. They save you from rewriting type definitions and keep everything consistent when the original changes.”
Code Example
interface User {
id: number
name: string
email: string
password: string
}
// All properties optional — great for update payloads
type UserUpdate = Partial<User>
// Keep only public fields
type PublicUser = Pick<User, 'id' | 'name'>
// Everything except the secret
type SafeUser = Omit<User, 'password'>
// Build a keyed map
type UsersById = Record<number, User>
function patch(id: number, changes: UserUpdate): SafeUser {
const existing: User = load(id)
const { password, ...safe } = { ...existing, ...changes }
return safe
}Follow-up Questions
- What is the difference between Pick and Omit?
- How would you implement Partial<T> yourself using mapped types?
- Is Readonly<T> shallow or deep, and how do you make it deep?
- When would you reach for Record<K, V>?
- What do Exclude and Extract do on union types?
MCQ Practice
1. Which utility type makes every property of T optional?
Partial<T> maps over every property and adds the optional modifier, producing a type where all fields may be omitted.
2. Which pair removes named keys from a type?
Omit<T, K> produces a type with the keys in K removed, while Pick<T, K> keeps only those keys.
3. What does Record<string, number> describe?
Record<K, V> builds an object type whose keys come from K and whose values all have type V.
Flash Cards
What does Partial<T> do? — Makes every property of T optional.
Pick vs Omit? — Pick<T, K> keeps only keys K; Omit<T, K> removes keys K.
What does Record<K, V> build? — An object type with keys K and values of type V.
Is Readonly<T> deep? — No — it is shallow; nested objects stay mutable.