TypeScript Utility Types That Reduce Boilerplate
Built-in utility types like Partial, Pick, and Record can eliminate redundant type definitions across your codebase.
Key Insights
- Partial
and Required let you derive flexible types from strict base types - Pick and Omit are essential for creating API-specific type subsets
- Record<K, V> replaces verbose index signature syntax
Deriving Types Instead of Duplicating
interface User {
id: string;
name: string;
email: string;
role: 'admin' | 'user';
createdAt: Date;
}
// Instead of redefining, derive:
type CreateUserInput = Omit<User, 'id' | 'createdAt'>;
type UpdateUserInput = Partial<Pick<User, 'name' | 'email' | 'role'>>;
Record for Clean Maps
type StatusCounts = Record<'active' | 'inactive' | 'pending', number>;
// Equivalent to { active: number; inactive: number; pending: number }
Extract and Exclude for Union Types
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
type ReadOnlyMethods = Extract<HttpMethod, 'GET'>; // 'GET'
type WriteMethods = Exclude<HttpMethod, 'GET'>; // 'POST' | 'PUT' | 'DELETE' | 'PATCH'