Partial, Required, Pick, and Omit
Built-in utility types transform existing types. Partial<T> makes properties optional; Required<T> makes them required; Pick<T, K> selects a subset of keys; Omit<T, K> removes selected keys. These are especially useful for create/update DTOs and separating public data from internal fields.
Utility types do not alter runtime objects. A Partial<User> is a compile-time description, not a function that removes or changes properties. Validate business rules separately: a type may permit an optional field while the domain still requires at least one field in a particular operation.
Create and update DTOs
type User = {
id: string;
name: string;
email: string;
createdAt: Date;
};
type CreateUserInput = Pick<User, "name" | "email">;
type UpdateUserInput = Partial<CreateUserInput>;
type PublicUser = Omit<User, "createdAt">;💡
Pick selects fields for creation, Partial allows updates to include only changed fields, and Omit removes createdAt from the public type.Key Takeaways
- Utility types transform types, not runtime data.
- Use Pick/Omit to define intentional API shapes.
- Partial is useful for patches but may need extra business validation.
Knowledge Check
What does `Partial<T>` do?