TypeScript Utility Types
Learn how to transform existing TypeScript types using built-in utilities such as Partial, Required, Readonly, Pick, Omit, Record and other useful types.
1. What are Utility Types?
TypeScript provides several built-in Utility Types that transform existing types into new type definitions.
For example, if an Employee type contains several required properties, the Partial utility can create a version where all properties are optional. The Pick utility can select only specific properties from the original type.
Basic Example
interface Employee {
id: number;
name: string;
department: string;
}
const employeeUpdate: Partial<Employee> = {
name: "Priya"
};
console.log(employeeUpdate.name);
Expected Output
Priya
The original Employee interface requires all three properties. Partial<Employee> makes each property optional, allowing an object to contain only the properties being updated.
2. Utility Types Workflow Diagram
Understand how Utility Types transform an existing type into a new type that fits a specific application requirement.
Existing Type
Define an interface or type alias with its properties.
Utility Type
Select, modify or transform the original type.
New Type
Obtain a modified version of the original type.
Use in Application
Use the new type for updates, responses or configuration.
3. Partial<T>
Partial makes all properties of a type optional. It is commonly used when updating only some properties of an object.
Example
interface Student {
id: number;
name: string;
age: number;
}
const studentUpdate: Partial<Student> = {
name: "Rahul"
};
console.log(studentUpdate.name);
console.log(studentUpdate.age);
Expected Output
Rahul
undefined
Only name is supplied. The other properties are optional in this Partial type, so they do not need to be present in the object.
4. Required<T>
Required makes all properties of a type required. It is useful when a type contains optional properties but a particular operation requires every property to be supplied.
Example
interface User {
name: string;
email?: string;
phone?: string;
}
const user1: Required<User> = {
name: "Priya",
email: "priya@example.com",
phone: "9876543210"
};
console.log(user1.name);
console.log(user1.email);
console.log(user1.phone);
Expected Output
Priya
priya@example.com
9876543210
5. Readonly<T>
Readonly makes all properties of a type readonly. It prevents their reassignment through normal TypeScript checking.
Example
interface Product {
id: number;
name: string;
price: number;
}
const product1: Readonly<Product> = {
id: 101,
name: "Keyboard",
price: 1200
};
console.log(product1.name);
console.log(product1.price);
// This would cause a TypeScript error:
// product1.price = 1500;
Expected Output
Keyboard
1200
The product properties can be read, but normal reassignment is disallowed by TypeScript. Readonly is a compile-time restriction, not a guarantee of runtime immutability.
6. Pick<T, K>
Pick creates a new type containing only the properties selected from an existing type. The selected property names must be keys of T.
Example
interface Employee {
id: number;
name: string;
department: string;
salary: number;
}
type EmployeeSummary = Pick<Employee, "id" | "name">;
const employee1: EmployeeSummary = {
id: 101,
name: "Amit"
};
console.log(employee1.id);
console.log(employee1.name);
Expected Output
101
Amit
How Pick Works
Employee
id, name, department and salary.
Pick
Select only id and name.
EmployeeSummary
Contains only the selected properties in its type.
7. Omit<T, K>
Omit creates a new type by excluding selected properties from an existing type. It is useful when a particular view or operation should not include certain properties.
Example
interface Employee {
id: number;
name: string;
department: string;
salary: number;
}
type PublicEmployee = Omit<Employee, "salary">;
const employee1: PublicEmployee = {
id: 101,
name: "Priya",
department: "IT"
};
console.log(employee1.name);
console.log(employee1.department);
Expected Output
Priya
IT
PublicEmployee excludes salary from its type. This type-level transformation alone does not remove salary from an existing runtime object or provide data security.
8. Record<K, T>
Record creates a type whose keys are K and whose values are T. It is useful for defining objects that map a fixed set of keys to values of a consistent type.
Example
type Course = "Java" | "Python" | "DotNet";
const courseFees: Record<Course, number> = {
Java: 25000,
Python: 22000,
DotNet: 28000
};
console.log(courseFees.Java);
console.log(courseFees.Python);
console.log(courseFees.DotNet);
Expected Output
25000
22000
28000
Record<Course, number> requires an entry for each of the three Course keys, and every corresponding value must be a number.
9. Exclude<T, U>
Exclude removes from a union type those members that are assignable to another specified type.
Example
type Status = "Pending" | "Completed" | "Cancelled";
type ActiveStatus = Exclude<Status, "Cancelled">;
const status1: ActiveStatus = "Pending";
const status2: ActiveStatus = "Completed";
console.log(status1);
console.log(status2);
// This would cause a TypeScript error:
// const status3: ActiveStatus = "Cancelled";
Expected Output
Pending
Completed
ActiveStatus contains only Pending and Completed because Cancelled has been removed from the union.
10. Extract<T, U>
Extract keeps only those union members that are assignable to another specified type. It works in the opposite direction to Exclude.
Example
type Status = "Pending" | "Completed" | "Cancelled";
type FinalStatus = Extract<Status, "Completed" | "Cancelled">;
const status1: FinalStatus = "Completed";
const status2: FinalStatus = "Cancelled";
console.log(status1);
console.log(status2);
Expected Output
Completed
Cancelled
FinalStatus includes only Completed and Cancelled. Pending is not part of the resulting type.
11. NonNullable<T>
NonNullable removes null and undefined from a type. It is useful when a value is expected to contain a usable value rather than represent a missing result.
Example
type UserName = string | null | undefined;
type ValidUserName = NonNullable<UserName>;
const userName: ValidUserName = "Rahul";
console.log(userName);
// This would cause a TypeScript error:
// const missingName: ValidUserName = null;
Expected Output
Rahul
ValidUserName is string because null and undefined have been removed. This type transformation does not clean or validate incoming data at runtime.
12. ReturnType<T>
ReturnType extracts the return type of a function type. It is useful when you want to reuse the result type of a function without defining that type separately.
Example
function getEmployee() {
return {
id: 101,
name: "Priya",
department: "IT"
};
}
type EmployeeResult = ReturnType<typeof getEmployee>;
const employee1: EmployeeResult = {
id: 102,
name: "Amit",
department: "Development"
};
console.log(employee1.id);
console.log(employee1.name);
console.log(employee1.department);
Expected Output
102
Amit
Development
typeof getEmployee refers to the function's type,
and ReturnType extracts the type of the object that function returns.
13. Parameters<T>
Parameters extracts a function's parameter types as a tuple. This can be useful when another function needs to reuse the same parameter structure.
Example
function createStudent(name: string, age: number) {
return {
name,
age
};
}
type StudentParameters = Parameters<typeof createStudent>;
const studentData: StudentParameters = ["Rahul", 22];
console.log(studentData[0]);
console.log(studentData[1]);
Expected Output
Rahul
22
StudentParameters becomes a tuple of [string, number], preserving the function's parameter order and types.
14. Utility Types Quick Reference
| Utility Type | Purpose | Common Use |
|---|---|---|
Partial<T> |
Makes properties optional. | Update requests. |
Required<T> |
Makes properties required. | Complete data objects. |
Readonly<T> |
Makes properties readonly. | Prevent reassignment during type checking. |
Pick<T, K> |
Selects specified properties. | Summary or view models. |
Omit<T, K> |
Excludes specified properties. | Derived data types. |
Record<K, T> |
Maps keys to a value type. | Lookup tables and configuration maps. |
Exclude<T, U> |
Removes matching union members. | Restricting allowed states. |
Extract<T, U> |
Keeps matching union members. | Selecting permitted states. |
NonNullable<T> |
Removes null and undefined. | Defining non-nullable values. |
ReturnType<T> |
Extracts a function's return type. | Reusing result types. |
Parameters<T> |
Extracts function parameter types as a tuple. | Reusing parameter structures. |
Summary
Utility Types transform existing TypeScript types into useful variations. Partial makes properties optional, Required makes them required, Readonly restricts reassignment, Pick selects properties, Omit excludes properties, and Record creates mapped object types. Exclude, Extract, NonNullable, ReturnType and Parameters help transform unions and extract function types.
Next, learn Type Narrowing and how TypeScript identifies a more specific type through conditions, type checks and control flow.