omit()
omit<
T,K>(object,keys):Omit<T,K>
omit<
T>(object,keys):Partial<T>
Creates a new object with specified keys omitted.
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โ
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} />;
}