Utility Types¶
TypeScript ships with a rich set of built‑in utility types.
They’re all defined using generics and mapped types, but you don’t need to understand their internals to use them effectively.
We’ll cover the most common and useful ones.
Partial<T>¶
Makes all properties optional.
Example¶
interface User {
id: number
name: string
email: string
}
type UserUpdate = Partial<User>
Equivalent to:
{
id?: number
name?: string
email?: string
}
When to use it¶
- Patch/update operations
- Form inputs
- Optional configuration objects
Required<T>¶
Makes all properties required (the opposite of Partial).
type CompleteUser = Required<User>
Useful when you want to enforce completeness after validation.
Readonly<T>¶
Makes all properties immutable.
type ImmutableUser = Readonly<User>
Great for:
- Redux state
- configuration objects
- preventing accidental mutation
Pick<T, K>¶
Selects a subset of properties.
type UserPreview = Pick<User, "id" | "name">
Equivalent to:
{
id: number
name: string
}
When to use it¶
- Lightweight views of large objects
- API responses that expose only some fields
- UI components that don’t need everything
Omit<T, K>¶
The opposite of Pick: removes properties.
type UserWithoutEmail = Omit<User, "email">
When to use it¶
- Remove sensitive fields
- Remove fields you want to compute dynamically
- Create “safe” versions of objects
Record<K, T>¶
Creates an object type with keys of type K and values of type T.
type ScoreMap = Record<string, number>
Equivalent to:
{
[key: string]: number
}
When to use it¶
- Dictionaries
- Maps
- Lookup tables
- Config objects
ReturnType<T>¶
Extracts the return type of a function.
function createUser() {
return { id: 1, name: "Alice" }
}
type User = ReturnType<typeof createUser>
Now User is:
{ id: number; name: string }
When to use it¶
- Avoid duplicating types
- Derive types from factory functions
- Keep return types in sync with implementation
Parameters<T>¶
Extracts a tuple of a function’s parameter types.
function log(x: string, y: number) {}
type Args = Parameters<typeof log>
// [string, number]
Useful for:
- wrapping functions
- decorators
- higher‑order functions
ConstructorParameters<T>¶
Same as Parameters, but for class constructors.
class Point {
constructor(public x: number, public y: number) {}
}
type PointArgs = ConstructorParameters<typeof Point>
// [number, number]
InstanceType<T>¶
Gets the instance type of a class.
type PointInstance = InstanceType<typeof Point>
Equivalent to:
{ x: number; y: number }
NonNullable<T>¶
Removes null and undefined.
type Clean = NonNullable<string | null | undefined>
// string
Great for narrowing and safe APIs.
Exclude<T, U>¶
Removes types from a union.
type Status = "loading" | "success" | "error"
type WithoutError = Exclude<Status, "error">
// "loading" | "success"
Extract<T, U>¶
Keeps only the overlapping types.
type OnlyError = Extract<Status, "error">
// "error"
When to Use Which Utility Type¶
Here’s a quick guide:
| Utility Type | Use When You Need To… |
|---|---|
| Partial | Make everything optional (updates, forms) |
| Required | Enforce completeness after validation |
| Readonly | Prevent mutation (state, config) |
| Pick | Select a subset of fields |
| Omit | Remove fields (sensitive or computed) |
| Record | Create dictionaries or maps |
| ReturnType | Derive types from functions |
| Parameters | Wrap or transform functions |
| InstanceType | Get the type of a class instance |
| Exclude | Remove union members |
| Extract | Keep only specific union members |
| NonNullable | Remove null/undefined |
This table alone will save you hours of typing and refactoring.
Putting It All Together¶
Here’s a realistic example using multiple utility types:
interface User {
id: string
name: string
email: string
password: string
}
// API response should not expose password
type PublicUser = Omit<User, "password">
// Update payload should allow partial fields
type UserUpdate = Partial<PublicUser>
// Map of users by ID
type UserMap = Record<string, PublicUser>
// Factory return type
function createUser(): PublicUser {
return { id: "1", name: "Alice", email: "a@example.com" }
}
type CreatedUser = ReturnType<typeof createUser>
This is exactly how real TypeScript applications model data safely and flexibly.
Summary¶
In this lesson, you learned the most important TypeScript utility types and when to use them:
1. Transforming object shapes¶
Partial,Required,Readonly,Pick,Omit
2. Creating flexible data structures¶
Record
3. Deriving types from functions and classes¶
ReturnType,Parameters,ConstructorParameters,InstanceType
4. Working with unions¶
Exclude,Extract,NonNullable
Utility types are the backbone of modern TypeScript—they let you build powerful abstractions with minimal code.