TypeScript provides a powerful set of utility types that enhance type safety, reduce redundancy, and improve code maintainability. These built-in utility types allow developers to transform, filter, and manipulate types effectively. Whether you're working with objects, unions, or function parameters, utility types can simplify complex type definitions.
In this post, weโll explore essential TypeScript utility types with real-world examples and best practices.
1๏ธโฃ Partial โ Make All Properties Optional
Partial<T>
creates a new type where all properties of T
become optional. This is useful for updating objects without requiring all properties.
Example:
type User = {
id: number;
name: string;
email: string;
};
function updateUser(user: User, updates: Partial<User>): User {
return { ...user, ...updates };
}
const user1: User = { id: 1, name: "Alice", email: "alice@example.com" };
const updatedUser = updateUser(user1, { name: "Bob" });
console.log(updatedUser); // { id: 1, name: "Bob", email: "alice@example.com" }
2๏ธโฃ Readonly โ Prevent Object Modification
Readonly<T>
ensures that all properties of an object cannot be changed after initialization.
Example:
type Settings = {
theme: string;
darkMode: boolean;
};
const config: Readonly<Settings> = { theme: "light", darkMode: false };
// config.darkMode = true; // โ Error: Cannot assign to 'darkMode' because it is a read-only property.
3๏ธโฃ Pick โ Select Specific Properties
Pick<T, K>
lets you extract a subset of properties from an existing type.
Example:
type User = {
id: number;
name: string;
email: string;
};
type UserPreview = Pick<User, "id" | "name">;
const userPreview: UserPreview = { id: 1, name: "Alice" };
// No 'email' property required
4๏ธโฃ Omit โ Exclude Specific Properties
Omit<T, K>
is the opposite of Pick
, allowing you to remove specific properties from a type.
Example:
type UserWithoutEmail = Omit<User, "email">;
const userWithoutEmail: UserWithoutEmail = { id: 1, name: "Alice" };
// No 'email' property allowed
5๏ธโฃ Record โ Create Mapped Types
Record<K, T>
helps define objects where keys are of type K
and values are of type T
.
Example:
type Role = "admin" | "editor" | "user";
const userRoles: Record<Role, number> = {
admin: 1,
editor: 2,
user: 3,
};
6๏ธโฃ Exclude & Extract โ Filter Union Types
-
Exclude<T, U>
removes specific types from a union. -
Extract<T, U>
keeps only the matching types from a union.
Example:
type Status = "success" | "error" | "pending";
type ActiveStatus = Exclude<Status, "error">; // "success" | "pending"
type ErrorStatus = Extract<Status, "error">; // "error"
7๏ธโฃ NonNullable โ Remove null
and undefined
NonNullable<T>
ensures a type doesnโt contain null
or undefined
.
Example:
type Name = string | null | undefined;
type ValidName = NonNullable<Name>; // string
๐ Conclusion
TypeScript utility types enhance code maintainability, reduce repetition, and improve type safety. Whether you need to make properties optional, readonly, or filter union types, these utilities are incredibly powerful.
By leveraging these built-in tools, you can write more efficient, error-free TypeScript code.
๐ก Which utility type do you use the most? Let me know in the comments! ๐
Top comments (0)