Skip to content

Commit d3e1ee8

Browse files
committed
fix(utils): omit uses readonly prefix for key list
1 parent 7fb8b94 commit d3e1ee8

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

packages/utils/src/omit.ts

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
type R = Record<string, unknown>;
2-
31
/**
4-
* I really don't know how to typedef this. It just creates a new object that
5-
* has all the values copied over except for any keys that are defined in the
6-
* omitKeys param.
2+
* Create a new object that does not contain the provided keys.
3+
*
4+
* @example
5+
* Simple Examples
6+
* ```ts
7+
* const object = {
8+
* a: "",
9+
* b: 3,
10+
* c: false,
11+
* 4: null,
12+
* } as const;
13+
*
14+
* expect(omit(object, ["a"])).toEqual({
15+
* b: 3,
16+
* c: false,
17+
* 4: null,
18+
* });
19+
* expect(omit(object, ["a", "c", "d"])).toEqual({ b: 3 });
20+
* ```
721
*
822
* @internal
923
* @param object - The object to remove keys from
@@ -12,17 +26,18 @@ type R = Record<string, unknown>;
1226
*/
1327
export function omit<T extends object, K extends keyof T>(
1428
object: T,
15-
omitKeys: K[] | string[]
29+
omitKeys: readonly (K | string)[]
1630
): Omit<T, K> {
1731
if (!omitKeys.length) {
1832
return object;
1933
}
2034

21-
return Object.keys(object).reduce((updated, key) => {
22-
if (!(omitKeys as string[]).includes(key)) {
23-
(updated as R)[key] = (object as R)[key];
35+
const result: Record<string, unknown> = {};
36+
for (const key in object) {
37+
if (!omitKeys.includes(key as unknown as K)) {
38+
result[key] = object[key];
2439
}
40+
}
2541

26-
return updated;
27-
}, {}) as Omit<T, K>;
42+
return result as Omit<T, K>;
2843
}

0 commit comments

Comments
 (0)