diff --git a/packages/utils/src/omit.ts b/packages/utils/src/omit.ts index f55010a36a..01a070d22f 100644 --- a/packages/utils/src/omit.ts +++ b/packages/utils/src/omit.ts @@ -1,9 +1,23 @@ -type R = Record; - /** - * I really don't know how to typedef this. It just creates a new object that - * has all the values copied over except for any keys that are defined in the - * omitKeys param. + * Create a new object that does not contain the provided keys. + * + * @example + * Simple Examples + * ```ts + * const object = { + * a: "", + * b: 3, + * c: false, + * 4: null, + * } as const; + * + * expect(omit(object, ["a"])).toEqual({ + * b: 3, + * c: false, + * 4: null, + * }); + * expect(omit(object, ["a", "c", "d"])).toEqual({ b: 3 }); + * ``` * * @internal * @param object - The object to remove keys from @@ -12,17 +26,18 @@ type R = Record; */ export function omit( object: T, - omitKeys: K[] | string[] + omitKeys: readonly (K | string)[] ): Omit { if (!omitKeys.length) { return object; } - return Object.keys(object).reduce((updated, key) => { - if (!(omitKeys as string[]).includes(key)) { - (updated as R)[key] = (object as R)[key]; + const result: Record = {}; + for (const key in object) { + if (!omitKeys.includes(key as unknown as K)) { + result[key] = object[key]; } + } - return updated; - }, {}) as Omit; + return result as Omit; }