omit

Returns a new object with the specified keys omitted.

1. Code

/**
 * Creates a new object with the specified keys omitted.
 *
 * @param obj - The object from which to omit keys.
 * @param keys - An array of keys to omit from the object.
 * @returns A new object with the specified keys omitted.
 */
const omit = <T, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> => {
  const newObj = { ...obj };
  keys.forEach((key) => delete newObj[key]);
  return newObj as Omit<T, K>;
};

export default omit;

2. Installation

npx @jrtilak/lazykit@latest add omit -ts

3. Description

The omit function returns a new object with the specified keys omitted. It does not modify the original object.

4. Props

Prop

Type

Default Value

object*object---
keys*array---

5. Examples

import omit from ".";

const obj = { a: 1, b: 2, c: 3 };
const keys = ["a", "c"] as Array<keyof typeof obj>;
// You have to specify the keys as an array of keyof typeof obj to ensure that the keys are valid, If you are defining the keys separately.

const omitted = omit(obj, keys);
console.log(omitted);
// Expected output: { b: 2 }

//OR
const omitted2 = omit(obj, ["a", "c"]);
console.log(omitted2);
// Expected output: { b: 2 }