Utility Types
Intro to Utility Types
TypeScript provides powerful utility types that let you transform and manipulate
types. These built-in helpers reduce boilerplate and make your types more
expressive.
type Event = { id: string; title: string; startsAt: Date; location?: string }
// Make all properties optional
type DraftEvent = Partial<Event>
// { id?: string; title?: string; startsAt?: Date; location?: string }
// Select specific properties
type EventSummary = Pick<Event, 'id' | 'title'>
// { id: string; title: string }
// Remove specific properties
type PublicEvent = Omit<Event, 'location'>
// { id: string; title: string; startsAt: Date }
Object Utility Types
| Utility | Purpose |
|---|---|
Partial<T> | Makes all properties optional |
Required<T> | Makes all properties required |
Readonly<T> | Makes all properties readonly |
Pick<T, K> | Selects specific properties |
Omit<T, K> | Removes specific properties |
Record<K, V> | Creates object with key type K |
Union Utility Types
| Utility | Purpose |
|---|---|
Exclude<T, U> | Remove types from union |
Extract<T, U> | Keep only types in union |
NonNullable<T> | Remove null and undefined |
Function Utility Types
| Utility | Purpose |
|---|---|
ReturnType<T> | Get function's return type |
Parameters<T> | Get function's parameter types |
Awaited<T> | Unwrap Promise type |
Utility types are type-level functions. They take types as input and produce
new types as output, enabling powerful type transformations without runtime
overhead.
In this exercise, you'll use utility types to transform and manipulate types
efficiently, then build your own with mapped types.