From cc4fd3d253a2d3280cfb135463da2319cdcc83a1 Mon Sep 17 00:00:00 2001 From: Ulad Kasach Date: Wed, 24 Apr 2024 08:00:32 -0400 Subject: [PATCH] fix(pick): ensure pick satisfies Pick standard type --- src/companions/pick.test.ts | 10 ++++++++++ src/companions/pick.ts | 9 ++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/companions/pick.test.ts diff --git a/src/companions/pick.test.ts b/src/companions/pick.test.ts new file mode 100644 index 0000000..e2160a1 --- /dev/null +++ b/src/companions/pick.test.ts @@ -0,0 +1,10 @@ +import { pick } from './pick'; + +describe('pick', () => { + it('should satisfy the standard Pick type', () => { + const withAll = { a: 1, b: 2 }; + + const pickKeyTypeFound = pick(withAll, ['a']); + const pickKeyTypeCheck: Pick = pickKeyTypeFound; + }); +}); diff --git a/src/companions/pick.ts b/src/companions/pick.ts index 3127f6b..a9c49c3 100644 --- a/src/companions/pick.ts +++ b/src/companions/pick.ts @@ -1,10 +1,13 @@ /** * a companion to the `Pick` type, returns a new object with only the picked attributes */ -export const pick = >( +export const pick = < + T extends Record, + K extends readonly (keyof T)[], +>( obj: T, - keys: (keyof T)[], -): Pick => + keys: K, +): Pick => Object.entries(obj).reduce((summary, [key, value]) => { // if key is not in the specified keys, dont add it to the new object if (!keys.includes(key)) return summary;