Skip to content

Commit

Permalink
chore: improve only typing
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Nov 24, 2024
1 parent 7d24ca2 commit d706c31
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,13 @@ function createCollectionOperations<T>(collection: Collection<T>): CollectionOpe
return collect(merged) as CollectionOperations<RecordMerge<T, U>>
},

only<K extends keyof T>(...keys: K[]) {
only<K extends string>(...keys: K[]) {
return this.map((item) => {
const result = {} as Pick<T, K>
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
})
Expand Down
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ export interface CollectionOperations<T> extends Collection<T> {
) => Map<K, V>
merge: <U extends T>(other: U[] | CollectionOperations<U>) => CollectionOperations<T | U>
mergeRecursive: <U>(other: U[] | CollectionOperations<U>) => CollectionOperations<RecordMerge<T, U>>
only: <K extends keyof T>(...keys: K[]) => CollectionOperations<Pick<T, K>>
only: <K extends string>(
...keys: K[]
) => CollectionOperations<{
[P in K & keyof T]?: T[P]
}>
pad: (size: number, value: T) => CollectionOperations<T>
pop: () => T | undefined
prepend: (value: T) => CollectionOperations<T>
Expand Down

0 comments on commit d706c31

Please sign in to comment.