TypeScript Utility Types Cheatsheet
Quick reference for TypeScript’s built-in utility types that I use frequently:
Pick and Omit
type User = {
id: string;
name: string;
email: string;
password: string;
};
// Pick only what you need
type PublicUser = Pick<User, 'id' | 'name'>;
// Omit what you don't want
type UserWithoutPassword = Omit<User, 'password'>;
Partial and Required
// All properties optional
type PartialUser = Partial<User>;
// All properties required
type RequiredUser = Required<User>;
Record
Great for typed objects:
type UserRoles = Record<string, 'admin' | 'user' | 'guest'>;
const roles: UserRoles = {
'user-1': 'admin',
'user-2': 'user',
};
ReturnType
Extract the return type of a function:
function getUser() {
return { id: '1', name: 'John' };
}
type User = ReturnType<typeof getUser>;
These utilities make TypeScript code more maintainable and DRY.