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

fix(pluck): pluck now can return object #71

Merged
merged 3 commits into from
Jan 20, 2024

Conversation

Nemo108
Copy link
Contributor

@Nemo108 Nemo108 commented Oct 13, 2023

The current version of pluck typing has a big mistake: it always returns an array, when it should return an object if the object is passed as a parameter.
This commit can fix this.

@Harris-Miller
Copy link
Collaborator

Thank you so much for this update. Looking at #61, I'm pretty sure I merged that by mistake. That MR was not ready.

types/pluck.d.ts Outdated
export function pluck<K extends keyof U, U>(prop: K, list: readonly U[]): Array<U[K]>;
export function pluck<U extends Record<any, any>, IK extends keyof any>(__: Placeholder, record: Record<IK, U>): <K extends keyof U>(prop: K) => { [KK in keyof typeof record]: U[K] };
export function pluck<U>(__: Placeholder, list: readonly U[]): <K extends keyof U>(prop: K) => Array<U[K]>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript always selects the last defined overload when you pass an overloaded function like a an argument to another function. That means if you called flip(pluck), flip would get the function pluck<U>(__: Placeholder, list: readonly U[]): <K extends keyof U>(prop: K) => Array<U[K]> type definition and return <U>(list: readonly U[], __: Placeholder) => <K extends keyof U>(prop: K) => Array<U[K]>, and that's not what we want.

The order was already correct, please re-order your changes to match the original order

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what I was thinking about when I was rearranging them. Thank you for being vigilant. I fixed that one and made the last overload as wide as possible.

types/pluck.d.ts Outdated Show resolved Hide resolved
types/pluck.d.ts Outdated
@@ -1,10 +1,10 @@
import { Placeholder } from 'ramda';

export function pluck<K extends PropertyKey>(prop: K extends Placeholder ? never : K): {
<U extends readonly unknown[] | Record<K, any>>(obj: Record<PropertyKey, U>): U extends readonly (infer T)[] ? T[] : U extends Record<K, infer T> ? T[] : never;
<U extends Record<K, any>, IK extends string>(obj: Record<IK, U>): { [KK in keyof typeof obj]: U[K] };
<U extends readonly unknown[] | Record<K, any>>(list: U[]): U extends readonly (infer T)[] ? T[] : U extends Record<K, infer T> ? T[] : never;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you will also need to apply U extends readonly (infer T)[] ? T[] : U extends Record<K, infer T> ? T[] that is here to your updated versions for when obj: Record<>. This would cover the case of { a: string[]; b: string[]; } or in other works, Record<string, string[]>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Done that

@Harris-Miller
Copy link
Collaborator

@Nemo108 I'm working on getting the next release out end of this week. Please fix remaining comments

@Nemo108
Copy link
Contributor Author

Nemo108 commented Jan 3, 2024

Hi! As far as I can see, I fixed every comment you have left to me. This PR is awaiting for your review

test/pluck.test.ts Outdated Show resolved Hide resolved
test/pluck.test.ts Show resolved Hide resolved
test/pluck.test.ts Outdated Show resolved Hide resolved
@Harris-Miller
Copy link
Collaborator

Harris-Miller commented Jan 3, 2024

@Nemo108 sorry I had left my comments in "Pending" state. I made them after your last update. You should be able to see them now

* restore the order of function overloads;
* make the last defined overload cover wider cases;
* add more tests to cover most of the cases.
@Nemo108
Copy link
Contributor Author

Nemo108 commented Jan 10, 2024

@Harris-Miller I'm sorry for the wait. I think I've done everithing you've asked me to do, can you check it one more time?

@Harris-Miller Harris-Miller merged commit fd618c0 into ramda:develop Jan 20, 2024
3 checks passed
@Nemo108 Nemo108 deleted the @nemo108/pluck-record branch January 21, 2024 09:48
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

Successfully merging this pull request may close these issues.

2 participants