Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Loosen the type on the Array sum function #419

Open
dyerw opened this issue Jul 26, 2024 · 2 comments
Open

[Request] Loosen the type on the Array sum function #419

dyerw opened this issue Jul 26, 2024 · 2 comments

Comments

@dyerw
Copy link

dyerw commented Jul 26, 2024

I don't see any reason why in sum the type param T has to extend object for the non-number case:

export function sum<T extends object>(

export function sum<T extends object>(
  array: readonly T[],
  fn: (item: T) => number
): number

so long as I can provide fn: (item: T) => number what does it matter that T extends object?

My specific use case is I have a normalized data object mapping ids to objects and I want to sum a list of ids like:

const lookup: Record<string, Data> = {...};

const ids: string[] = [...];

_.sum(ids, (id) => lookup[id].numberField);

Now I could map to objects first sure, but arguably I could want to sum the length of an array of strings, or say the number of trues in a list of booleans like _.sum(listOfBools, (b) => b ? 1 : 0)

@MarlonPassos-git
Copy link
Contributor

@dyerw You could choose to use radashi which is a fork of radash and in it we fixed this problem

@aleclarson
Copy link

Hey @dyerw, as Marlon said, this issue is fixed in radashi@beta (stable release coming very soon). If you'd like to know how Radashi (the fork) is different from Radash, I recently wrote a little bit about that: radashi-org/radashi#168 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants