If youβve been using TypeScript for a while, youβve probably written repetitive types β like copying an interface just to make all fields optional or removing one key from another type.
Thatβs where TypeScript Utility Types come in.
Theyβre built-in helpers that let you transform existing types instead of creating new ones from scratch.
In this post, weβll explore the most useful ones β with real examples you can instantly apply in your React, NextJS, Node.js, or MERN projects.
π§ 1. Partial<Type> β Make All Properties Optional
The
Partialutility type turns all properties of a given type into optional.
interface User {
id: number;
name: string;
email: string;
}
type UpdateUser = Partial<User>;
const updateUser: UpdateUser = {
name: "John",
};
π’ When to use:
When youβre creating βupdateβ forms or API payloads that donβt require all fields.
βοΈ 2. Pick<Type, Keys> β Select Specific Properties
Pick lets you choose only certain properties from a type.
interface User {
id: number;
name: string;
email: string;
password: string;
}
type PublicUser = Pick<User, "id" | "name">;
const user: PublicUser = {
id: 1,
name: "John",
};
π’ When to use:
When you need to expose only public data (e.g., remove sensitive info like passwords).
π« 3. Omit<Type, Keys> β Exclude Certain Properties
The opposite of Pick. It creates a type by omitting specific properties.
type SafeUser = Omit<User, "password">;
const safeUser: SafeUser = {
id: 1,
name: "john",
email: "john@example.com",
};
π’ When to use:
When you want to reuse an existing type but hide specific fields.
π 4. Readonly<Type> β Prevent Modification
Marks all properties of a type as read-only.
interface Config {
apiUrl: string;
version: number;
}
const config: Readonly<Config> = {
apiUrl: "https://api.example.com",
version: 1,
};
// β Error: Cannot assign to 'apiUrl' because it is a read-only property.
config.apiUrl = "https://new-api.example.com";
π’ When to use:
For constant configurations or environment settings.
ποΈ 5. Record<Keys, Type> β Create an Object Type Dynamically
The Record utility constructs an object type with a set of keys and a uniform type.
type Roles = "admin" | "user" | "guest";
const permissions: Record<Roles, boolean> = {
admin: true,
user: true,
guest: false,
};
π’ When to use:
When you need to map a fixed set of keys (like roles, statuses, or categories) to values.
π§© 6. Required<Type> β Make All Properties Mandatory
Turns all properties of a type into required.
interface Settings {
theme?: string;
language?: string;
}
type StrictSettings = Required<Settings>;
const settings: StrictSettings = {
theme: "dark",
language: "en",
};
π’ When to use:
When you want to enforce that all fields must exist, even if they were optional before.
π§° Bonus: Combine Utility Types
You can mix them for more powerful transformations.
type UserPreview = Readonly<Pick<User, "id" | "name">>;
const userPreview: UserPreview = {
id: 1,
name: "John",
};
// userPreview.id = 2 β Error (readonly)
π’ When to use:
When you want fine-grained control over types β like a read-only subset of user data.
Utility types are small but powerful tools that make TypeScript a joy to work with.
Once you start using them, youβll rarely find yourself rewriting similar interfaces again.
Hereβs a quick recap:
| Utility Type | Purpose |
|---|---|
Partial<T> |
Make all properties optional |
Pick<T, K> |
Select specific keys |
Omit<T, K> |
Exclude specific keys |
Readonly<T> |
Make all properties immutable |
Record<K, T> |
Create a type with specific keys and value types |
Required<T> |
Make all properties required |
If you enjoyed this guide, you can find more tutorials on my portfolio blog π
Top comments (2)
Thank you for sharing this article!
I found the explanations clear, and the examples made it easy to understand.
I'll keep these in mind.
Thanks a lot! π
Iβm really glad the examples helped you understand it better.
Iβll be posting more TypeScript and React content soon β stay tuned!