
Common Utility Types of TypeScript

Khalid Kakar
Khalid simplifies complex modern web development topics into simple words with expertise in Next.js and React.js
Hello TypeScripters 😊
In this artical you will learn basic and common Typescript Utility types such as Partial<T> Required<T> Pick<T, K> and more. 🔥
1. Partial<T> – Make All Properties Optional 🤢
The Partial<T> utility type takes a type T and makes all its properties optional. This is useful when updating objects where only a subset of properties may be provided.
interface User {
name: string;
age: number;
}
interface PartialUser = Partial<User>
// { name?:string, age?:number }2. Required<T> – Make All Properties Required 🤷♂️
The Required<T> utility type does the opposite of Partial<T> by ensuring that all properties of T are required.
interface User {
name?: string;
age?: number;
}
interface RequiredUser = Required<User>
// { name:string, age:number }3. Readonly<T> – Make All Properties Read-Only 👏
The Readonly<T> utility type prevents properties from being modified after initialization.
interface User {
name: string;
}
const user: Readonly<User> = { name: "Khalid" };
// user.name = "khan"; ❌ Error: Cannot assign to 'name' because it is a read-only property.4. Pick<T, K> – Select Specific Properties 😁
The Pick<T, K> utility type extracts a subset of properties from T.
interface User {
id: number;
name: string;
email: string;
}
interface NewUser = Pick<User, 'id' | 'name'>
// { id: number; name: string; }5. Omit<T, K> – Remove Specific Properties 😉
The Omit<T, K> utility type removes specified properties from T.
interface User {
id: number;
name: string;
email: string;
}
interface UserWithOutEmail = Omit<User, 'email'>
// { id: number; name: string; }6. Record<K, T> – Define a Key-Value Object Type 🙌
The Record<K, T> utility type creates an object type where keys are K and values are T.
type Role = "admin" | "user" | "guest";
interface NewRoles = Record<Role, string>
// { admin: string; user: string; guest: string; }7. Extract<T, U> – Extract Common Types 🤞
The Extract<T, U> utility type extracts types from T that are also present in U.
type Status = "success" | "error" | "pending";
type AllowedStatus = Extract<Status, "success" | "error">
// "success" | "error"8. Exclude<T, U> – Remove Certain Types 🤔
The Exclude<T, U> utility type removes types from T that match U.
type Status = "success" | "error" | "pending";
type NotPending = Exclude<Status, "pending">;
// "success" | "error"Happy Types 😍😍🎉