From 9b54ed83acf323e9ac6651942452a3ef000b377c Mon Sep 17 00:00:00 2001 From: Sunny Zanchi Date: Thu, 21 Apr 2022 22:50:42 -0400 Subject: [PATCH] Use generic for Record key type This lets the function pass through more specific type information --- packages/object-map/index.d.ts | 10 +++++----- packages/object-map/index.test.ts | 8 +++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/object-map/index.d.ts b/packages/object-map/index.d.ts index db492586f..477a47add 100644 --- a/packages/object-map/index.d.ts +++ b/packages/object-map/index.d.ts @@ -1,6 +1,6 @@ -declare function map( - item: Record, - callback: (key: string, value: T) => U -): Record; +declare function map( + item: Record, + callback: (key: K, value: T) => U, +): Record -export default map; +export default map diff --git a/packages/object-map/index.test.ts b/packages/object-map/index.test.ts index eccb96fbc..59e2c29d8 100644 --- a/packages/object-map/index.test.ts +++ b/packages/object-map/index.test.ts @@ -13,10 +13,16 @@ const test3: Record = 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 = map({ foo: 1 }, (_, value) => value + 1); +const test5: Record = map({ foo: 1 }, (_, value) => value + 1); // @ts-expect-error map(); // @ts-expect-error