Aller au contenu principal

omit()

omit<T, K>(object, keys): Omit<T, K>

omit<T>(object, keys): Partial<T>

Creates a new object with specified keys omitted.

remarque

Overload with PropertyKey[] returns Partial<T> for dynamic keys.


Type Parameters

T: T extends Record<PropertyKey, any>

The type of the input object.

K: K extends string | number | symbol

The type of the keys to omit.


Parameters

Overload 1:

object: T

The object to omit keys from.

keys: readonly K[]

Array of keys to omit.

Overload 2:

object: T

The object to omit keys from.

keys: readonly PropertyKey[]

Array of keys to omit.


Returns: Omit<T, K>

A new object without the specified keys.


See Also

pick


Since

2.0.0


Performance

O(n + k) time & space where n is object size, k is number of keys to omit. Uses spread operator then delete operations.


Also known as

omit (Lodash, es-toolkit, Remeda, Radashi, Ramda, Effect, Modern Dash) · ❌ (Antfu)


Example

const obj = { a: 1, b: 2, c: 3 };

omit(obj, ['b']); // => { a: 1, c: 3 }
omit(obj, ['a', 'c']); // => { b: 2 }

// Remove sensitive data
const user = { id: 1, name: 'John', password: 'secret' };
omit(user, ['password']); // => { id: 1, name: 'John' }

// Dynamic keys (Object.keys)
const keysToOmit = Object.keys({ b: true, c: true });
omit(obj, keysToOmit); // => { a: 1 }

How it works?

Creates a new object excluding the specified keys.

Exclusion Process

Sensitive Data Removal


Use Cases

Exclude specific properties 📌

Create a shallow copy of an object excluding specified keys. Essential for efficient shallow removals or removing sensitive data.

const publicUser = omit(user, ['password', 'secretToken']);
sendToClient(publicUser);

Sanitize internal fields

Remove framework-specific or internal properties before serialization. Useful for cleaning ORM entities or decorated objects.

const cleanEntity = omit(dbRecord, ['_id', '__v', 'createdAt']);
return JSON.stringify(cleanEntity);

Filter invalid keys

Remove known invalid or deprecated keys from an object hash.

const cleanParams = omit(params, ['legacyParam1', 'unused_flag']);

Strip undefined values before API call

Remove undefined or null fields from a partial form before sending to an API. Very common with optional form fields where you don't want to send empty values.

const formData = { name: "Alice", bio: undefined, avatar: null, email: "alice@example.com" };

const cleanPayload = omit(formData, ["bio", "avatar"]);
// => { name: "Alice", email: "alice@example.com" }
await api.updateProfile(cleanPayload);

Remove non-DOM props before spreading in React

Strip custom props that shouldn't be forwarded to the underlying HTML element. Essential pattern for React wrapper components using spread props.

interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant: "primary" | "secondary";
isLoading: boolean;
}

function Button({ variant, isLoading, ...rest }: ButtonProps) {
// variant and isLoading are already destructured,
// but for components that forward all props:
const domProps = omit(props, ["variant", "isLoading", "onAnalyticsClick"]);
return <button {...domProps} />;
}