Skip to content

Commit

Permalink
fix: Simplify keyBy type.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh committed Nov 25, 2024
1 parent aea5b45 commit 910afe3
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/array/keyBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ declare global {
}
}

Array.prototype.keyBy = function <
T,
K extends PropertyKey,
TK extends keyof T,
TKK extends T[TK] extends K ? TK : never,
Y = T,
>(this: T[], fnOrKey: CallbackFn<T, K> | TKK, valueFn?: CallbackFn<T, Y>) {
Array.prototype.keyBy = function <T, K extends PropertyKey, Y = T>(
this: T[],
fnOrKey: CallbackFn<T, K> | keyof T[][number],
valueFn?: CallbackFn<T, Y>,
) {
const result = {} as Record<K, Y>;
const fn = typeof fnOrKey === "function" ? fnOrKey : (x: T) => x[fnOrKey] as K;
this.forEach((e, i, a) => {
Expand Down

0 comments on commit 910afe3

Please sign in to comment.