Series: TypeScript from Basics to Advanced
Part: 10 — Utility Types
Level: Beginner → Intermediate
Introduction
In Part 9, we learned about Generics.
Generics allow us to create reusable types and functions.
For example:
function identity<T>(value: T): T {
return value;
}
We can use the same function with:
string
number
boolean
objects
arrays
But TypeScript gives us another powerful capability.
What if we already have a type and want to create a modified version of that type?
For example:
interface User {
id: number;
name: string;
email: string;
password: string;
}
Now imagine we need:
-
A version where every property is optional.
-
A version where every property is readonly.
-
A version containing only
idandname. -
A version without
password. -
A version where only certain union values are allowed.
-
A type representing the return value of a function.
We could manually create every type.
But that would create unnecessary duplication.
TypeScript provides Utility Types to solve this problem.
Utility types are built-in generic types that allow us to transform existing types into new types.
In this article, we'll learn:
-
What Utility Types are
-
Partial<T> -
Required<T> -
Readonly<T> -
Pick<T, K> -
Omit<T, K> -
Record<K, T> -
Exclude<T, U> -
Extract<T, U> -
NonNullable<T> -
ReturnType<T> -
Parameters<T> -
Awaited<T> -
Combining Utility Types
-
Practical React examples
-
Practical API examples
-
Common mistakes
-
Best practices
-
Hands-on exercises with answers
1. What Are Utility Types?
Utility Types are built-in TypeScript types that help us transform existing types.
Instead of creating a new type manually:
interface UserUpdate {
name?: string;
email?: string;
password?: string;
}
we can write:
interface User {
name: string;
email: string;
password: string;
}
type UserUpdate = Partial<User>;
Partial<User> automatically makes every property optional.
This is the basic idea behind Utility Types.
2. Why Are Utility Types Useful?
Consider this type:
interface User {
id: number;
name: string;
email: string;
password: string;
}
A real application might need many variations:
User
UserCreate
UserUpdate
UserResponse
UserPreview
ReadonlyUser
PublicUser
Manually maintaining all of these can become difficult.
Utility Types allow us to derive them from one source type.
For example:
type UserUpdate = Partial<User>;
or:
type PublicUser = Omit<User, "password">;
This keeps our types more consistent.
3. Partial<T>
Partial<T> makes all properties optional.
Consider:
interface User {
id: number;
name: string;
email: string;
}
Normally:
const user: User = {
id: 1,
name: "Sundar",
email: "sundar@example.com"
};
All properties are required.
Now:
type UserUpdate = Partial<User>;
This effectively becomes:
{
id?: number;
name?: string;
email?: string;
}
So:
const update: UserUpdate = {
name: "Rao"
};
is valid.
4. Practical Partial Example
Partial is extremely useful for update operations.
Suppose we have:
interface User {
id: number;
name: string;
email: string;
phone: string;
}
An update API might allow the user to change only one field.
Instead of:
function updateUser(
id: number,
name: string,
email: string,
phone: string
) {
// ...
}
we can write:
function updateUser(
id: number,
updates: Partial<User>
) {
// ...
}
Now:
updateUser(1, {
name: "Rao"
});
is valid.
And:
updateUser(1, {
email: "new@example.com",
phone: "9999999999"
});
is also valid.
5. Required<T>
Required<T> does the opposite of Partial<T>.
It makes all properties required.
Consider:
interface User {
id: number;
name: string;
email?: string;
}
Here email is optional.
Now:
type RequiredUser = Required<User>;
makes:
id → required
name → required
email → required
Therefore:
const user: RequiredUser = {
id: 1,
name: "Sundar",
email: "sundar@example.com"
};
is valid.
But:
const user: RequiredUser = {
id: 1,
name: "Sundar"
};
is invalid because email is required.
6. Readonly<T>
Readonly<T> makes all properties readonly.
Consider:
interface User {
id: number;
name: string;
}
Now:
type ReadonlyUser = Readonly<User>;
The resulting type behaves like:
{
readonly id: number;
readonly name: string;
}
For example:
const user: ReadonlyUser = {
id: 1,
name: "Sundar"
};
This is invalid:
user.name = "Rao";
because name is readonly.
7. Partial vs Required vs Readonly
These three utility types are frequently used together.
Partial<T>
Makes properties optional:
Partial<User>
Required<T>
Makes properties required:
Required<User>
Readonly<T>
Prevents property reassignment:
Readonly<User>
They don't change the original type.
They create a new type based on the original type.
8. Pick<T, K>
Pick<T, K> creates a type containing only selected properties.
Consider:
interface User {
id: number;
name: string;
email: string;
password: string;
}
Suppose we only need:
id
name
We can write:
type UserPreview = Pick<
User,
"id" | "name"
>;
Now UserPreview contains:
{
id: number;
name: string;
}
9. Practical Pick Example
Imagine a user profile page only needs:
name
email
We can write:
type UserProfile = Pick<
User,
"name" | "email"
>;
Now:
const profile: UserProfile = {
name: "Sundar",
email: "sundar@example.com"
};
The password property isn't part of the type.
10. Pick with keyof
Pick is closely related to keyof.
Suppose:
interface User {
id: number;
name: string;
email: string;
}
Then:
keyof User
produces:
"id" | "name" | "email"
Therefore:
Pick<User, "id" | "name">
means:
Pick these valid keys from
User.
This is one reason understanding keyof from the Generics article is important.
11. Omit<T, K>
Omit<T, K> is essentially the opposite of Pick.
Instead of selecting properties, it removes properties.
Consider:
interface User {
id: number;
name: string;
email: string;
password: string;
}
Suppose we don't want to expose the password.
We can write:
type PublicUser = Omit<
User,
"password"
>;
Now:
{
id: number;
name: string;
email: string;
}
12. Practical Omit Example
This is particularly useful for API responses.
Suppose our database model is:
interface User {
id: number;
name: string;
email: string;
password: string;
createdAt: Date;
}
We don't want to expose the password to the frontend.
We can create:
type UserResponse = Omit<
User,
"password"
>;
Now:
const response: UserResponse = {
id: 1,
name: "Sundar",
email: "sundar@example.com",
createdAt: new Date()
};
This makes the intended public shape explicit.
Note that TypeScript types do not remove fields from actual runtime objects. Your API/server must still avoid sending sensitive fields.
13. Pick vs Omit
Use:
Pick<T, K>
when you know what you want to keep.
Use:
Omit<T, K>
when you know what you want to remove.
For example:
type UserPreview = Pick<
User,
"id" | "name"
>;
and:
type PublicUser = Omit<
User,
"password"
>;
Both are useful depending on the situation.
14. Record<K, T>
We already saw Record in Part 8.
Record<K, T> creates an object type whose keys are K and whose values are T.
For example:
type Scores = Record<
string,
number
>;
This means:
Any string key
↓
Must contain a number
For example:
const scores: Scores = {
Sundar: 90,
Rahul: 85,
Priya: 95
};
15. Record with Specific Keys
Record becomes especially useful with union types.
type Role =
| "admin"
| "user"
| "moderator";
Now:
type Permissions = Record<
Role,
string[]
>;
The object must contain:
admin
user
moderator
For example:
const permissions: Permissions = {
admin: [
"read",
"write",
"delete"
],
user: [
"read"
],
moderator: [
"read",
"write"
]
};
16. Exclude<T, U>
Exclude<T, U> removes types from a union.
For example:
type Status =
| "pending"
| "success"
| "failed";
Suppose we want everything except "failed".
We can write:
type ActiveStatus = Exclude<
Status,
"failed"
>;
The resulting type is:
"pending" | "success"
17. Practical Exclude Example
Consider:
type Role =
| "admin"
| "user"
| "moderator";
Suppose a function should accept every role except admin.
We can write:
type NonAdminRole = Exclude<
Role,
"admin"
>;
Now:
const role: NonAdminRole = "user";
is valid.
But:
const role: NonAdminRole = "admin";
is invalid.
18. Extract<T, U>
Extract<T, U> does the opposite of Exclude.
It keeps only types that are assignable to U.
For example:
type Status =
| "pending"
| "success"
| "failed";
We can write:
type SuccessfulStatus = Extract<
Status,
"success"
>;
The result is:
"success"
19. Extract with Multiple Types
Suppose:
type Status =
| "pending"
| "success"
| "failed"
| "cancelled";
We can extract several values:
type CompletedStatus = Extract<
Status,
"success" | "failed"
>;
The result is:
"success" | "failed"
20. NonNullable<T>
NonNullable<T> removes:
null
undefined
from a type.
For example:
type Value =
| string
| null
| undefined;
Now:
type SafeValue =
NonNullable<Value>;
becomes:
string
21. Practical NonNullable Example
Consider:
interface User {
id: number;
name: string;
}
type UserOrNull =
User | null;
Now:
type ValidUser =
NonNullable<UserOrNull>;
becomes:
User
This is useful when creating types that represent values after null checks or guaranteed initialization.
22. ReturnType<T>
ReturnType<T> extracts the return type of a function.
Consider:
function getUser() {
return {
id: 1,
name: "Sundar"
};
}
We can write:
type User = ReturnType<
typeof getUser
>;
TypeScript infers:
{
id: number;
name: string;
}
This is useful when you want a type based directly on an existing function's return value.
23. Understanding typeof with ReturnType
Notice:
ReturnType<typeof getUser>
Why do we use typeof?
Because ReturnType expects a function type.
typeof getUser obtains the type of the function itself.
Then:
ReturnType<typeof getUser>
extracts what that function returns.
This pattern is very common in advanced TypeScript code.
24. Parameters<T>
Parameters<T> extracts the parameter types of a function as a tuple.
Consider:
function createUser(
name: string,
age: number
) {
return {
name,
age
};
}
We can write:
type CreateUserParams =
Parameters<typeof createUser>;
The result is:
[string, number]
This is a tuple containing the function's parameter types in order.
25. Practical Parameters Example
Suppose:
function login(
email: string,
password: string
) {
// login
}
We can extract:
type LoginParams =
Parameters<typeof login>;
Now LoginParams is:
[string, string]
We can use it elsewhere:
const args: LoginParams = [
"sundar@example.com",
"password123"
];
login(...args);
26. Awaited<T>
Awaited<T> extracts the resolved value from a Promise.
Consider:
async function getUser() {
return {
id: 1,
name: "Sundar"
};
}
The function returns:
Promise<{
id: number;
name: string;
}>
We can extract the resolved value:
type User = Awaited<
ReturnType<typeof getUser>
>;
Now User represents:
{
id: number;
name: string;
}
This is particularly useful with asynchronous API functions.
27. Practical API Example with Awaited
Consider:
async function fetchUsers() {
const response = await fetch(
"/api/users"
);
return response.json() as Promise<User[]>;
}
We can derive the resolved result:
type Users =
Awaited<ReturnType<typeof fetchUsers>>;
The result is:
User[]
This avoids manually duplicating the return type.
28. Combining Utility Types
Utility Types become especially powerful when combined.
Consider:
interface User {
id: number;
name: string;
email: string;
password: string;
phone?: string;
}
Suppose we want:
-
No
password -
All remaining properties optional
We can write:
type UserUpdate = Partial<
Omit<User, "password">
>;
Now:
const update: UserUpdate = {
name: "Rao"
};
is valid.
This is a very common real-world pattern.
29. Combining Pick and Readonly
Suppose we want only:
id
name
and we don't want them to be modified.
We can write:
type UserPreview = Readonly<
Pick<User, "id" | "name">
>;
Now:
const user: UserPreview = {
id: 1,
name: "Sundar"
};
Both properties are readonly.
30. Combining Required and Pick
Suppose:
interface User {
id: number;
name: string;
email?: string;
}
We want a type containing:
name
email
where both are required.
We can write:
type CompleteContact =
Required<
Pick<User, "name" | "email">
>;
Now both properties are required.
31. Utility Types with Generics
Utility Types themselves are heavily based on generics.
For example:
Partial<User>
means:
T = User
Similarly:
Pick<User, "id" | "name">
uses generic parameters to determine:
T → User
K → "id" | "name"
This is why understanding Generics makes Utility Types much easier to understand.
32. Practical React Example
Utility Types are very useful when creating React component props.
Suppose:
interface User {
id: number;
name: string;
email: string;
avatar: string;
}
A component may only need:
type UserCardProps =
Pick<User, "name" | "avatar">;
Now:
function UserCard(
props: UserCardProps
) {
return (
<div>
<img src={props.avatar} />
<h2>{props.name}</h2>
</div>
);
}
The component receives only the properties it actually needs.
33. Practical React Form Example
Suppose:
interface User {
id: number;
name: string;
email: string;
phone: string;
}
For an edit form, we may not want the id.
We can create:
type UserFormData =
Omit<User, "id">;
Now:
const formData: UserFormData = {
name: "Sundar",
email: "sundar@example.com",
phone: "9999999999"
};
This is cleaner than creating another interface manually.
34. Practical Node.js API Example
Suppose we have:
interface User {
id: number;
name: string;
email: string;
password: string;
}
For creating a user:
type CreateUser =
Omit<User, "id">;
For updating a user:
type UpdateUser =
Partial<
Omit<User, "id">
>;
For returning a user to the frontend:
type UserResponse =
Omit<User, "password">;
One base interface gives us several useful types.
35. Utility Types for Online Test Applications
Let's apply Utility Types to an online test application.
Suppose:
interface Question {
id: string;
question: string;
options: string[];
correctAnswer: string;
explanation: string;
}
When sending a question to a student, we don't want to expose:
correctAnswer
or potentially:
explanation
We can create:
type StudentQuestion =
Omit<
Question,
"correctAnswer" | "explanation"
>;
Now the frontend receives only the information required to display the question.
36. Utility Types for Test Updates
Suppose:
interface Test {
id: string;
title: string;
duration: number;
questions: Question[];
}
For updating a test:
type UpdateTest =
Partial<
Omit<Test, "id">
>;
Now:
const update: UpdateTest = {
title: "TypeScript Assessment"
};
is valid.
37. Common Mistakes
Mistake 1: Recreating types manually
Instead of:
interface UserUpdate {
name?: string;
email?: string;
}
when the source already exists, consider:
type UserUpdate =
Partial<
Pick<User, "name" | "email">
>;
This keeps the derived type synchronized with the source type.
Mistake 2: Confusing Pick and Omit
Remember:
Pick → Keep selected properties
Omit → Remove selected properties
Mistake 3: Thinking Readonly freezes objects at runtime
This:
Readonly<User>
is a TypeScript type-level restriction.
It does not automatically call:
Object.freeze()
at runtime.
Mistake 4: Assuming Omit removes runtime properties
This:
type PublicUser =
Omit<User, "password">;
doesn't remove password from an actual JavaScript object.
You still need to control what your application sends or stores at runtime.
Mistake 5: Overusing Utility Types
Utility Types are powerful, but deeply nested combinations can become difficult to understand.
For example:
Partial<
Readonly<
Pick<
Omit<User, "id">,
"name" | "email"
>
>
>
This is technically valid, but if the type becomes difficult to read, a named type may be better.
38. Best Practices
1. Derive types from a source of truth
Instead of duplicating types, derive related types:
type UserResponse =
Omit<User, "password">;
2. Use Partial for update operations
Partial<User>
is ideal when any subset of properties can be updated.
3. Use Pick for focused data
Use it when a component or function only needs a few properties.
4. Use Omit for removing sensitive or irrelevant fields
For example:
Omit<User, "password">
5. Use Record for dictionary-like objects
Record<string, number>
is concise and readable.
6. Use ReturnType and Parameters when types should follow functions
This reduces duplicated function signatures.
7. Don't make utility-type combinations unnecessarily complicated
If a derived type becomes difficult to understand, give it a descriptive name or create a clearer explicit type.
39. Quick Mental Model
A useful way to remember the major Utility Types is:
Partial
↓
Make everything optional
Required
↓
Make everything required
Readonly
↓
Make everything readonly
Pick
↓
Keep selected properties
Omit
↓
Remove selected properties
Record
↓
Create key-value object types
Exclude
↓
Remove members from a union
Extract
↓
Keep matching members from a union
NonNullable
↓
Remove null and undefined
ReturnType
↓
Get a function's return type
Parameters
↓
Get a function's parameter types
Awaited
↓
Get the resolved value of a Promise
40. Hands-On Exercises
Now let's practice Utility Types.
Exercise 1: Partial
Create:
interface User {
id: number;
name: string;
email: string;
}
Create a UserUpdate type where every property is optional.
Exercise 2: Required
Create:
interface Product {
id: number;
name: string;
description?: string;
}
Create a type where every property is required.
Exercise 3: Readonly
Create a readonly version of:
interface User {
id: number;
name: string;
}
Then try changing the name.
Exercise 4: Pick
Given:
interface User {
id: number;
name: string;
email: string;
phone: string;
}
Create a type containing only:
id
name
Exercise 5: Omit
Using the same User interface, create a type without:
phone
Exercise 6: Record
Create a type called Scores where:
key → string
value → number
Then create an object containing scores for three students.
Exercise 7: Exclude
Given:
type Status =
| "pending"
| "success"
| "failed";
Create a type that excludes:
failed
Exercise 8: Extract
Using the same Status type, create a type that contains only:
success
failed
Exercise 9: ReturnType
Create:
function getProduct() {
return {
id: 1,
name: "Laptop",
price: 50000
};
}
Use ReturnType to create the product type automatically.
Exercise 10: Parameters
Create:
function createUser(
name: string,
age: number,
email: string
) {
// ...
}
Use Parameters to extract the function parameter types.
Exercise 11: Awaited
Create an asynchronous function that returns:
{
id: number;
name: string;
}
Use:
Awaited
together with:
ReturnType
to extract the resolved object type.
Exercise 12: Combine Utility Types
Given:
interface User {
id: number;
name: string;
email: string;
password: string;
}
Create a UserUpdate type that:
-
Does not contain
id -
Does not contain
password -
Makes everything else optional
41. Answers & Solutions
Answer 1
interface User {
id: number;
name: string;
email: string;
}
type UserUpdate =
Partial<User>;
Now:
const update: UserUpdate = {
name: "Rao"
};
is valid.
Answer 2
interface Product {
id: number;
name: string;
description?: string;
}
type CompleteProduct =
Required<Product>;
Now description is required.
Answer 3
interface User {
id: number;
name: string;
}
type ReadonlyUser =
Readonly<User>;
const user: ReadonlyUser = {
id: 1,
name: "Sundar"
};
This is invalid:
user.name = "Rao";
Answer 4
type UserPreview =
Pick<
User,
"id" | "name"
>;
The resulting type contains only:
id
name
Answer 5
type UserWithoutPhone =
Omit<User, "phone">;
Answer 6
type Scores =
Record<string, number>;
const scores: Scores = {
Sundar: 90,
Rahul: 85,
Priya: 95
};
Answer 7
type Status =
| "pending"
| "success"
| "failed";
type ActiveStatus =
Exclude<Status, "failed">;
Result:
"pending" | "success"
Answer 8
type CompletedStatus =
Extract<
Status,
"success" | "failed"
>;
Result:
"success" | "failed"
Answer 9
function getProduct() {
return {
id: 1,
name: "Laptop",
price: 50000
};
}
type Product =
ReturnType<typeof getProduct>;
Product is automatically inferred from the function's return value.
Answer 10
function createUser(
name: string,
age: number,
email: string
) {
return {
name,
age,
email
};
}
type CreateUserParams =
Parameters<typeof createUser>;
The resulting type is:
[string, number, string]
Answer 11
async function getUser() {
return {
id: 1,
name: "Sundar"
};
}
type User =
Awaited<
ReturnType<typeof getUser>
>;
User becomes:
{
id: number;
name: string;
}
Answer 12
interface User {
id: number;
name: string;
email: string;
password: string;
}
type UserUpdate =
Partial<
Omit<User, "id" | "password">
>;
Now:
const update: UserUpdate = {
name: "Rao"
};
is valid.
And:
const update: UserUpdate = {
email: "new@example.com"
};
is also valid.
But:
const update: UserUpdate = {
password: "123456"
};
is invalid because password was removed from the type.
42. Bonus Challenge
Create the following interface:
interface Test {
id: string;
title: string;
duration: number;
questions: string[];
createdAt: Date;
updatedAt: Date;
}
Now create:
CreateTest
It should:
-
Remove
id -
Remove
createdAt -
Remove
updatedAt
UpdateTest
It should:
-
Remove
id -
Remove
createdAt -
Remove
updatedAt -
Make all remaining properties optional
TestPreview
It should contain only:
-
id -
title -
duration
ReadonlyTest
It should make the entire test readonly.
A good solution is:
type CreateTest =
Omit<
Test,
"id" | "createdAt" | "updatedAt"
>;
type UpdateTest =
Partial<CreateTest>;
type TestPreview =
Pick<
Test,
"id" | "title" | "duration"
>;
type ReadonlyTest =
Readonly<Test>;
This demonstrates how Utility Types can be composed to create multiple application-specific types from a single source type.
43. Real-World Type Transformation
Consider a typical full-stack application.
You might start with:
interface User {
id: number;
name: string;
email: string;
password: string;
phone: string;
createdAt: Date;
}
From this one type, you can derive:
type CreateUser =
Omit<User, "id" | "createdAt">;
For updating:
type UpdateUser =
Partial<
Omit<User, "id" | "createdAt">
>;
For public API responses:
type PublicUser =
Omit<User, "password">;
For a small profile card:
type UserCard =
Pick<User, "name" | "phone">;
For immutable data:
type ImmutableUser =
Readonly<User>;
This is the real power of Utility Types.
Instead of maintaining many independent interfaces, we can derive related types from one source.
44. Generics + Utility Types
Utility Types become even more powerful when combined with Generics.
For example:
function updateEntity<T>(
entity: T,
updates: Partial<T>
): T {
return {
...entity,
...updates
};
}
Now the function works with many types.
For users:
const user = {
id: 1,
name: "Sundar",
email: "sundar@example.com"
};
updateEntity(user, {
name: "Rao"
});
For products:
const product = {
id: 10,
name: "Laptop",
price: 50000
};
updateEntity(product, {
price: 45000
});
The combination of:
Generics
+
Utility Types
↓
Reusable type-safe abstractions
is one of the most important patterns in advanced TypeScript.
Final Takeaway
Utility Types allow us to transform existing types without manually rewriting them.
The most important ones to remember are:
Partial<T>
Makes all properties optional.
Required<T>
Makes all properties required.
Readonly<T>
Makes all properties readonly.
Pick<T, K>
Keeps selected properties.
Omit<T, K>
Removes selected properties.
Record<K, T>
Creates a key-value object type.
Exclude<T, U>
Removes members from a union.
Extract<T, U>
Keeps matching members from a union.
NonNullable<T>
Removes null and undefined.
ReturnType<T>
Extracts a function's return type.
Parameters<T>
Extracts a function's parameter types.
Awaited<T>
Extracts the resolved value of a Promise.
The most important thing isn't memorizing every utility type.
Understand the purpose:
Need optional properties?
→ Partial
Need required properties?
→ Required
Need readonly properties?
→ Readonly
Need specific properties?
→ Pick
Need to remove properties?
→ Omit
Need dynamic key-value objects?
→ Record
Need to remove union members?
→ Exclude
Need matching union members?
→ Extract
Need to remove null/undefined?
→ NonNullable
Need a function's return type?
→ ReturnType
Need function parameters?
→ Parameters
Need the resolved Promise value?
→ Awaited
Once you understand these patterns, you'll start seeing them everywhere in professional TypeScript codebases.