Omit
Build a new type by removing certain properties from another type.
Syntax
Omit<User, "password">Omit<T, Keys> is the opposite of Pick. It creates a new type with the named properties removed. This is great for hiding sensitive fields like passwords.
Example
Loading editorβ¦
Press Run to execute the code.
When to use it
- A CreateUserDTO omits the id field from User because the id is assigned by the database, not the caller.
- An edit form type uses Omit<Post, 'id' | 'createdAt'> to allow editing content fields without touching metadata.
- A public DTO omits the password field from a User to prevent sensitive data from leaking into API responses.
More examples
Omit a sensitive field
Omit<User, 'password'> produces a type without the password field, and destructuring creates a matching runtime object.
interface User {
id: number;
name: string;
email: string;
password: string;
}
type SafeUser = Omit<User, 'password'>;
// { id: number; name: string; email: string }
function toDTO(user: User): SafeUser {
const { password, ...rest } = user;
return rest;
}Omit server-assigned fields
Removes server-generated fields so callers of createPost only need to supply the user-authored content.
interface Post {
id: number;
title: string;
body: string;
createdAt: Date;
}
type CreatePostInput = Omit<Post, 'id' | 'createdAt'>;
function createPost(input: CreatePostInput): Post {
return { ...input, id: Math.random(), createdAt: new Date() };
}Omit vs Pick comparison
Contrasts Pick and Omit on the same interface to show that they are complementary β choose whichever requires fewer key names.
interface Record {
id: number;
name: string;
value: number;
meta: string;
}
// Include two fields
type Picked = Pick<Record, 'id' | 'name'>;
// Exclude two fields (same result when there are four total)
type Omitted = Omit<Record, 'value' | 'meta'>;
// Both are: { id: number; name: string }
Discussion