Skip to content

Instantly share code, notes, and snippets.

@njbmartin
Created April 4, 2021 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njbmartin/9e4a9be5354b488e546ccf5fa5e8d6de to your computer and use it in GitHub Desktop.
Save njbmartin/9e4a9be5354b488e546ccf5fa5e8d6de to your computer and use it in GitHub Desktop.
const person = {
firstName: "Jane",
lastName: "Bloggs",
email: "jane@example.com",
phone: "0123456789"
};
const omitKey = <T, K extends keyof T>(data: T, key: K) => {
const { [key]: _, ...o } = data;
return o;
}
console.log(person);
// expected output: email is a property
const personWithoutEmail = omitKey(person, "email")
console.log(personWithoutEmail)
// expected output: email is not a property
// actual output: email is not a property and not part of the object... yay!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment