Pick Use Cases: API Response Subsets
What you'll learn: How to use Pick to create focused types from larger API response models for different UI contexts.
When working with APIs, you often receive large objects with many fields, but different parts of your application only need specific subsets of that data. Pick lets you extract exactly what you need, creating smaller, more focused types.
The Real-World Problem
Imagine your API returns a complete user object with dozens of fields:
type FullUser = {
id: string;
email: string;
passwordHash: string;
firstName: string;
lastName: string;
avatarUrl: string;
lastLoginAt: Date;
createdAt: Date;
updatedAt: Date;
billingAddress: string;
shippingAddress: string;
phoneNumber: string;
};
Your user profile component only needs a few fields. Instead of using the full type, you can pick exactly what you need:
type UserProfile = Pick<FullUser, 'id' | 'firstName' | 'lastName' | 'avatarUrl'>;
// Now UserProfile only has these four properties
Common API Subset Patterns
Dashboard summary:
type UserSummary = Pick<FullUser, 'id' | 'email' | 'lastLoginAt'>;
Public profile (safe to display):
type PublicUser = Pick<FullUser, 'id' | 'firstName' | 'lastName' | 'avatarUrl'>;
List view preview:
type UserListItem = Pick<FullUser, 'id' | 'email' | 'createdAt'>;
This approach keeps your components tightly typed without exposing unnecessary fields, improves autocomplete, and makes it clear which data each part of your app depends on.
Key Takeaway: Use Pick to create focused, context-specific types from large API response models, improving type safety and code clarity in different parts of your application.