Skip to content

Commit b3fac98

Browse files
author
Zanchi
committed
Use generic for Record key type
This lets the function pass through more specific type information
1 parent 3fd356a commit b3fac98

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

packages/object-map/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
declare function map<T, U = T>(
2-
item: Record<string, T>,
3-
callback: (key: string, value: T) => U
4-
): Record<string, U>;
1+
declare function map<K extends string | number | symbol, T, U = T>(
2+
item: Record<K, T>,
3+
callback: (key: K, value: T) => U
4+
): Record<K, U>
55

66
export default map;

packages/object-map/index.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ const test3: Record<string, string> = map(
1313
{ a: "pple" },
1414
(key, value) => key + value
1515
);
16+
type MyObject = {
17+
k1: number,
18+
k2: number,
19+
}
20+
const myObject: MyObject = { k1: 1, k2: 2 };
21+
const test4: MyObject = map(myObject, (key: keyof MyObject, value) => value + 1);
1622

1723
// Not OK
1824
// @ts-expect-error
19-
const test4: Record<string, string> = map({ foo: 1 }, (_, value) => value + 1);
25+
const test5: Record<string, string> = map({ foo: 1 }, (_, value) => value + 1);
2026
// @ts-expect-error
2127
map();
2228
// @ts-expect-error

0 commit comments

Comments
 (0)