Last active
February 18, 2022 12:50
-
-
Save imran-vz/f76c3e5371e2ed2731f4c559746d6b8c to your computer and use it in GitHub Desktop.
Fully typed function to omit/remove keys from an object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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