Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generic for object-map Record key type #448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/object-map/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare function map<T, U = T>(
item: Record<string, T>,
callback: (key: string, value: T) => U
): Record<string, U>;
declare function map<K extends string | symbol, T, U = T>(
item: Record<K, T>,
callback: (key: K, value: T) => U,
): Record<K, U>

export default map;
export default map
8 changes: 7 additions & 1 deletion packages/object-map/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ const test3: Record<string, string> = map(
{ a: "pple" },
(key, value) => key + value
);
type MyObject = {
k1: number,
k2: number,
}
const myObject: MyObject = { k1: 1, k2: 2 };
const test4: MyObject = map(myObject, (key: keyof MyObject, value) => value + 1);

// Not OK
// @ts-expect-error
const test4: Record<string, string> = map({ foo: 1 }, (_, value) => value + 1);
const test5: Record<string, string> = map({ foo: 1 }, (_, value) => value + 1);
// @ts-expect-error
map();
// @ts-expect-error
Expand Down