Combining Pick and Record
What you'll learn: How to chain Pick or Omit with Record to build complex, derived types from base models in a type-safe way.
The Power of Composition
You've learned Pick to extract specific properties and Record to create uniform object types. Now you can combine them to create sophisticated type transformations. Think of it like a two-step assembly line: first, you select exactly which parts you need from a base model, then you use those parts to construct a new, uniform structure.
The Pattern
The typical pattern looks like this:
type User = {
id: number;
name: string;
email: string;
age: number;
};
// Pick specific keys, then create a Record mapping them to a status
type UserFieldStatus = Record<keyof Pick<User, 'name' | 'email'>, boolean>;
// Result: { name: boolean; email: boolean }
Here, Pick<User, 'name' | 'email'> creates { name: string; email: string }, then keyof extracts 'name' | 'email', and finally Record maps each key to boolean.
Real-World Example: Validation Tracking
type Product = {
id: string;
title: string;
price: number;
stock: number;
category: string;
};
// Track which user-editable fields have validation errors
type EditableFieldErrors = Record<
keyof Pick<Product, 'title' | 'price' | 'stock'>,
string | null
>;
const errors: EditableFieldErrors = {
title: null,
price: "Must be positive",
stock: null
};
Key Takeaway: Combining Pick/Omit with Record lets you derive complex types by first selecting relevant properties, then mapping them to a uniform value type—perfect for validation states, field tracking, or configuration objects.