What Are TypeScript Utility Types?
TypeScript ships with a set of built-in generic types that transform other types. These utility types let you derive new types from existing ones without code duplication. Mastering them dramatically reduces the need for manual type maintenance.
Object Modification Utilities
Partial<T> — Makes all properties optional:
interface User {
id: number;
name: string;
email: string;
}
type UserUpdate = Partial<User>;
// { id?: number; name?: string; email?: string; }
async function updateUser(id: number, data: Partial<User>) {
await db.user.update({ where: { id }, data });
}Required<T> — Makes all properties required (opposite of Partial):
type CompleteUser = Required<UserUpdate>;
// { id: number; name: string; email: string; }Readonly<T> — Makes all properties read-only:
type ImmutableUser = Readonly<User>;
const user: ImmutableUser = { id: 1, name: 'Alice', email: 'a@example.com' };
user.name = 'Bob'; // Error: Cannot assign to 'name' because it is a read-only property.Property Selection Utilities
Pick<T, K> — Keeps only the specified keys:
type UserPreview = Pick<User, 'id' | 'name'>;
// { id: number; name: string; }
// Useful for API responses that don't need all fields
async function getUserPreview(id: number): Promise<UserPreview> {
return db.user.findUnique({ where: { id }, select: { id: true, name: true } });
}Omit<T, K> — Removes the specified keys:
type CreateUserInput = Omit<User, 'id'>;
// { name: string; email: string; }
type PublicUser = Omit<User, 'email'>;
// { id: number; name: string; }Record and Union Utilities
Record<K, V> — Creates an object type with keys K and values V:
type RolePermissions = Record<'admin' | 'user' | 'guest', string[]>;
const permissions: RolePermissions = {
admin: ['read', 'write', 'delete'],
user: ['read', 'write'],
guest: ['read'],
};Exclude<T, U> and Extract<T, U> — Filter union types:
type Status = 'pending' | 'active' | 'deleted';
type ActiveStatus = Exclude<Status, 'deleted'>;
// 'pending' | 'active'
type VisibleStatus = Extract<Status, 'pending' | 'active'>;
// 'pending' | 'active'NonNullable<T> — Removes null and undefined:
type MaybeString = string | null | undefined;
type DefiniteString = NonNullable<MaybeString>; // stringFunction Utilities
ReturnType<T> — Extracts the return type of a function:
async function getUser() {
return { id: 1, name: 'Alice', email: 'a@example.com' };
}
type GetUserResult = Awaited<ReturnType<typeof getUser>>;
// { id: number; name: string; email: string; }Parameters<T> — Extracts function parameter types as a tuple:
function createUser(name: string, email: string, role: 'admin' | 'user') {}
type CreateUserParams = Parameters<typeof createUser>;
// [name: string, email: string, role: 'admin' | 'user']Awaited<T>
New in TypeScript 4.5, Awaited<T> recursively unwraps Promise types:
type A = Awaited<Promise<string>>; // string
type B = Awaited<Promise<Promise<number>>>; // numberFor a deep dive into generics that power these utility types, see our TypeScript Generics guide.
