Omit<T, K>
Constructs a type by picking all properties from T and then removing K, the inverse of Pick.
Definition
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;Examples
interface User {
id: number;
name: string;
email: string;
password: string;
ssn: string;
}
type SafeUser = Omit<User, "password" | "ssn">;
// { id: number; name: string; email: string; }
function sanitize(user: User): SafeUser {
const { password, ssn, ...safe } = user;
return safe;
}interface BaseProps {
id: string;
className: string;
style: React.CSSProperties;
}
interface CardProps extends Omit<BaseProps, "id"> {
title: string;
children: React.ReactNode;
}
// CardProps has: className, style, title, children
// 'id' is intentionally excludedinterface BlogPost {
id: number;
title: string;
content: string;
authorId: number;
createdAt: Date;
updatedAt: Date;
}
type CreatePostDTO = Omit<BlogPost, "id" | "createdAt" | "updatedAt">;
const newPost: CreatePostDTO = {
title: "Hello World",
content: "My first post",
authorId: 42,
};Common Use Cases
- 1Removing auto-generated fields for create DTOs
- 2Stripping sensitive data before API responses
- 3Extending interfaces while overriding specific properties
- 4Creating form types from entity types
- 5Building insert types from full table schemas
Understanding Omit<T, K>
Omit<T, K> constructs a type by copying all properties from T and then removing the properties specified in K. It is implemented using a combination of Pick and Exclude: it picks from T all keys that are not in K.
Omit is extremely common in real-world TypeScript code. One of its most frequent uses is creating data transfer objects (DTOs) from entity types. When inserting a new record into a database, auto-generated fields like id, createdAt, and updatedAt should not be included. Instead of maintaining a separate CreateDTO interface, you can define it as Omit<Entity, "id" | "createdAt" | "updatedAt">.
In React development, Omit is essential when extending component interfaces. If you're wrapping a base component and want to override how a particular prop works, you can extend Omit<BaseProps, "conflictingProp"> and redefine the prop with a different type. This avoids type conflicts from interface merging.
Omit is also valuable for security. When returning user data from an API, sensitive fields like passwords, tokens, or internal IDs should be excluded. Typing your API response as Omit<User, "password" | "token"> ensures these fields cannot accidentally leak into the response.
One subtle difference from Pick is that Omit's K constraint is keyof any rather than keyof T. This means you can omit keys that don't exist on T without getting an error. While convenient, this can mask typos, so some teams prefer a stricter StrictOmit type that constrains K to keyof T.
Related Types
Pick<T, K>Constructs a type by picking the set of properties K from T, creating a subset of the original type.
Exclude<T, U>Constructs a type by excluding from T all union members that are assignable to U.
Partial<T>Constructs a type with all properties of T set to optional, allowing you to provide only a subset of the properties.
Record<K, T>Constructs an object type whose property keys are K and whose property values are T, useful for dictionaries and lookup maps.
More Mapping Types
Explore TypeScript Types
Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.