From d706c31c7f0a2af4585210a9145a3a9faca4482b Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 24 Nov 2024 23:18:33 +0100 Subject: [PATCH] chore: improve `only` typing --- src/collect.ts | 8 +++++--- src/types.ts | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/collect.ts b/src/collect.ts index 72562c8..175b0c5 100644 --- a/src/collect.ts +++ b/src/collect.ts @@ -349,11 +349,13 @@ function createCollectionOperations(collection: Collection): CollectionOpe return collect(merged) as CollectionOperations> }, - only(...keys: K[]) { + only(...keys: K[]) { return this.map((item) => { - const result = {} as Pick + const result = {} as { [P in K & keyof T]?: T[P] } keys.forEach((key) => { - result[key] = item[key] + if (key in item) { + result[key as keyof T] = item[key as keyof T] + } }) return result }) diff --git a/src/types.ts b/src/types.ts index 09768ff..0f14c0c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -236,7 +236,11 @@ export interface CollectionOperations extends Collection { ) => Map merge: (other: U[] | CollectionOperations) => CollectionOperations mergeRecursive: (other: U[] | CollectionOperations) => CollectionOperations> - only: (...keys: K[]) => CollectionOperations> + only: ( + ...keys: K[] + ) => CollectionOperations<{ + [P in K & keyof T]?: T[P] + }> pad: (size: number, value: T) => CollectionOperations pop: () => T | undefined prepend: (value: T) => CollectionOperations