-
Notifications
You must be signed in to change notification settings - Fork 19
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
groupBy returns a type that is not compatible with Object.entries #91
Comments
This is why we added the Consider the example used in the Ramda docs: const byGrade = R.groupBy(function(student) {
const score = student.score;
return score < 65 ? 'F' :
score < 70 ? 'D' :
score < 80 ? 'C' :
score < 90 ? 'B' : 'A';
});
const students = [{name: 'Abby', score: 84},
{name: 'Eddy', score: 58},
// ...
{name: 'Jack', score: 69}];
byGrade(students);
// {
// 'A': [{name: 'Dianne', score: 99}],
// 'B': [{name: 'Abby', score: 84}]
// // ...,
// 'F': [{name: 'Eddy', score: 58}]
// } The type of the return could be typed as type Grades = Record<'A' | 'B' | 'C' | 'D' | 'F', Student[]> The operation And it's for this very reason that MR wrapped the return in That being said... The Because for either I've been considering reverting that MR so the behavior of @jeysal contributed that MR, and I'd like their opinion on this as well |
Hi, im at FOSDEM this weekend so things are a bit hectic, but I'll try to read through and get back to you next week! :) |
I thought about this over night at realized you're right. The problem is |
Yes, this was what i was referring to. Thanks! |
Ok so this seems to essentially come down to the philosophic question of whether Bear in mind though that |
Also, there might be something else strange going on? If I try |
Interestingly enough, the issue is when a string is used as opposed to a union type. |
That is interesting indeed. It is using the same |
Regardless, to me |
Ironically, typescript is had this same discussion in the MR to support |
Looking at the typescript MR for Object.groupBy, they are returning |
Given how we now know that There still an odd difference between union keys @nemanja-tosic Thank you for bringing the issue to our attention none-the-less. It made for a god piece of conversation |
Stumbled upon this problem recently and, as I'm relatively new to TypeScript, I'm having a bit of trouble wrapping my head around it. My use case is quite simple and doesn't rely on union types for the keys, here's a fictional, simplified example: type Item = {name: string, category: string}
const items = [...]
R.groupBy(R.prop('category'), items) Intuitively I expected the last expression to return a |
@fuadsaud from your example, originally the return type was We're keeping the Playground of your example: https://tsplay.dev/N78bDm |
Do you have any recommendations for usage? It's unfortunate that a I understand this point, about a return type of I was under the impression that with I'm no TS expert though so I could be totally wrong here. |
@AndrewSouthpaw Your problem isn't unique to ramda, it will be true if you use Object.values(byColor).map((grouping: Foo[]) => {
console.log(grouping);
}); The main problem there is you manually typed Object.values(byColor).map(grouping => {
console.log(grouping);
});
Regardless of the correct typing, you're right in that However! Understand that the The only solution there is is just to check for |
Given the following groupBy invocation using version 0.29.10 of types
when using entries and strict like so
the messages variable type is
T[] | undefined
forcing us to do an extra check, but it shouldn't be possible for it to be undefined since we're iterating over object properties.This PR introduces the change - #45 that leads to the possible undefined.
The text was updated successfully, but these errors were encountered: