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} />;
}
Strip tracking params before sharing URLsβ
Remove UTM and tracking parameters from URLs before users share them. Essential for privacy-focused apps and clean link sharing.
const rawParams = { page: "products", category: "shoes", utm_source: "google", utm_medium: "cpc", fbclid: "abc123" };
const cleanParams = omit(rawParams, ["utm_source", "utm_medium", "utm_campaign", "fbclid", "gclid"]);
// => { page: "products", category: "shoes" }
const cleanUrl = `${baseUrl}?${new URLSearchParams(cleanParams)}`;