Careeroza
Pick and Omit
Selecting Properties
Pick<T, Keys> keeps only the listed properties. Omit<T, Keys> keeps everything except them. Both build new types from one source of truth, so a change to User flows into all of them.
Example
type User = {
id: number;
name: string;
email: string;
passwordHash: string;
createdAt: string;
};
type PublicUser = Omit<User, "passwordHash">;
type UserPreview = Pick<User, "id" | "name">;
type CreateUserInput = Omit<User, "id" | "createdAt" | "passwordHash"> & { password: string };
function toPublic({ passwordHash, ...rest }: User): PublicUser {
return rest;
}
const user: User = { id: 1, name: "Isha", email: "[email protected]", passwordHash: "$2b$10$abc", createdAt: "2026-01-15T10:00:00Z" };
const preview: UserPreview = { id: user.id, name: user.name };
const input: CreateUserInput = { name: "New", email: "[email protected]", password: "secret123" };
console.log(toPublic(user));
console.log(preview, Object.keys(input));Output
{
id: 1,
name: 'Isha',
email: '[email protected]',
createdAt: '2026-01-15T10:00:00Z'
}
{ id: 1, name: 'Isha' } [ 'name', 'email', 'password' ]Leaking a Field
Example
Trying to return the password hash where PublicUser is expected is caught by the excess property check (TS2353).
type User = { id: number; name: string; passwordHash: string };
type PublicUser = Omit<User, "passwordHash">;
const u: PublicUser = { id: 1, name: "Isha", passwordHash: "$2b$10$abc" };Compiler output (tsc)
Line 4: error TS2353: Object literal may only specify known properties, and 'passwordHash' does not exist in type 'PublicUser'.