Skip to content

Instantly share code, notes, and snippets.

@imran-vz
Last active February 18, 2022 12:50
Show Gist options
  • Save imran-vz/f76c3e5371e2ed2731f4c559746d6b8c to your computer and use it in GitHub Desktop.
Save imran-vz/f76c3e5371e2ed2731f4c559746d6b8c to your computer and use it in GitHub Desktop.
Fully typed function to omit/remove keys from an object.
export function omit<T extends Record<string, any>, K extends Array<keyof T>>(object: T, keys: K): Omit<T, keyof K> {
if (!object) return {} as T;
if (!keys) return object;
const objectKeys = Object.keys(object) as Array<keyof T>;
return objectKeys
.filter((key) => !keys.includes(key))
.reduce((obj, key) => {
obj[key] = object[key];
return obj;
}, {} as T);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment