Omit<T, K>: Excluding Properties
You'll learn how to use the Omit<T, K> utility type to create new types by removing specific properties from existing types.
What Omit Does
Omit is the inverse of Pick. While Pick lets you select properties you want, Omit lets you remove properties you don't want. Think of it like creating a redacted document—you start with everything and black out the parts you want to hide.
The syntax is Omit<T, K> where T is your source type and K is a string literal union of property names to exclude.
Basic Syntax
type User = {
id: string;
name: string;
email: string;
password: string;
};
// Omit sensitive fields
type PublicUser = Omit<User, "password">;
// Result: { id: string; name: string; email: string; }
// Omit multiple properties
type UserPreview = Omit<User, "password" | "email">;
// Result: { id: string; name: string; }
Common Use Cases
API Responses: Remove backend-only fields before sending data to clients:
type DatabaseUser = {
id: string;
username: string;
passwordHash: string;
createdAt: string;
};
type ApiUser = Omit<DatabaseUser, "passwordHash">;
Form Handling: Exclude auto-generated fields when creating new records:
type Product = {
id: string;
name: string;
price: number;
createdAt: string;
};
type ProductInput = Omit<Product, "id" | "createdAt">;
// User only provides name and price
Omit vs Pick
Use Omit when it's shorter to list what you don't want. Use Pick when it's shorter to list what you do want.
Key Takeaway: Omit<T, K> creates a type-safe copy of T with specified properties removed, perfect for hiding sensitive data or excluding auto-generated fields.