Web Development
TypeScript
Subjective
Oct 04, 2025
What are utility types in TypeScript? Provide examples.
Detailed Explanation
Utility types are built-in type transformations that help manipulate existing types.
Common Utility Types:
• Partial: Makes all properties optional
interface User {
id: number;
name: string;
email: string;
}
type PartialUser = Partial; // All properties optional
• Required: Makes all properties required
type RequiredUser = Required; // All properties required
• Pick: Selects specific properties
type UserSummary = Pick; // Only id and name
• Omit: Excludes specific properties
type UserWithoutEmail = Omit; // Excludes email
• Record: Creates object type with specific keys and values
type UserRoles = Record; // { [key: string]: boolean }
Benefits:
• Reduce code duplication
• Create variations of existing types
• Maintain type safety during transformations.
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts