Skip to content

Utility Types in TypeScript

Utility types are built-in generic types that transform existing types into new types. They save you from writing repetitive type transformations.

interface User {
name: string;
age: number;
email: string;
}
// All properties become optional
type PartialUser = Partial<User>;
// { name?: string; age?: number; email?: string }
function updateUser(id: string, updates: Partial<User>): void {
// Only updates provided fields
}
interface Config {
url?: string;
timeout?: number;
}
// All properties become required
type StrictConfig = Required<Config>;
// { url: string; timeout: number }
type ImmutableUser = Readonly<User>;
// { readonly name: string; readonly age: number; readonly email: string }
type UserName = Pick<User, "name" | "email">;
// { name: string; email: string }
type UserWithoutEmail = Omit<User, "email">;
// { name: string; age: number }
type PageInfo = {
title: string;
url: string;
};
type Pages = Record<string, PageInfo>;
// { [key: string]: { title: string; url: string } }
function createUser() {
return { id: 1, name: "Alice" };
}
type UserType = ReturnType<typeof createUser>;
// { id: number; name: string }
function greet(name: string, age: number): void {}
type GreetParams = Parameters<typeof greet>;
// [name: string, age: number]
// Form state management
interface FormState {
name: string;
email: string;
age: number;
terms: boolean;
}
type FormErrors = Partial<Record<keyof FormState, string>>;
type FormTouched = Record<keyof FormState, boolean>;
// API response transformation
type ApiUser = {
id: number;
name: string;
email: string;
password_hash: string;
created_at: string;
};
type PublicUser = Omit<ApiUser, "password_hash">;
type CreateUserPayload = Omit<ApiUser, "id" | "created_at">;

Easy: What does Partial<T> do?

Medium: What’s the difference between Pick and Omit?

Hard: How would you use ReturnType with complex functions?