TypeScript provides built-in global utility types that perform common type transformations on object shapes, functions, and unions.
Partial<T>: Makes all properties in T optional.Required<T>: Makes all properties in T required.Readonly<T>: Makes all properties in T read-only.Record<K, T>: Constructs an object type with key type K and value type T.Pick<T, K>: Constructs a type by picking specific keys K from T.Omit<T, K>: Constructs a type by omitting specific keys K from T.ReturnType<T>: Extracts the return type of a function type T.interface UserProfile {
id: string;
username: string;
email: string;
age: number;
bio?: string;
}
// Partial<T>: Ideal for update DTOs
type UpdateUserProfileDto = Partial<UserProfile>;
function updateUser(id: string, updates: UpdateUserProfileDto): void {
console.log(`Updating user ${id} with:`, updates);
}
updateUser("usr_100", { bio: "Full stack developer" });
// Pick<T, K> and Omit<T, K>
type PublicUserPreview = Pick<UserProfile, "id" | "username">;
type CreateUserPayload = Omit<UserProfile, "id">;
const newPayload: CreateUserPayload = {
username: "charlie",
email: "[email protected]",
age: 28
};
// Record<K, T>: Dynamic dictionary map
type FeatureFlags = Record<"enableMetrics" | "enableDarkMode" | "betaAccess", boolean>;
const userFlags: FeatureFlags = {
enableMetrics: true,
enableDarkMode: false,
betaAccess: true
};
console.log("Flags:", userFlags);
Readonly<Partial<T>> for immutable optional updates.Omit to Remove Auto-Generated Database IDs: Ideal when separating database entities from creation DTOs.ReturnType<typeof func> for Inferred Return Types: Extract returntypes from external libraries without manually duplicating types.Given an interface Todo { id: number; title: string; completed: boolean; }, create a type TodoPreview using Omit that excludes completed.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With