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

Call signature of take can prevent type inferrence when using curried signature #69

Open
TClark1011 opened this issue Oct 13, 2023 · 4 comments

Comments

@TClark1011
Copy link

TClark1011 commented Oct 13, 2023

The current typing of the take function is this:

export function take(n: number): {
  (xs: string): string;
  <T>(xs: readonly T[]): T[];
};
export function take(n: number, xs: string): string;
export function take<T>(n: number, xs: readonly T[]): T[];

Right now, when you call the curried signature inside a pipe, the type of the array is not correctly inferred:

image You can see the type inference is working correctly without the `R.take` call: image

The solution is to change the curried signature for taking from an array to this:

export function take<T>(n:number): (xs: readonly T[]) => T[]

This also applies to takeLast.

I would be happy to open a PR for this.

@Harris-Miller
Copy link
Collaborator

The problem you have called out is not isolated to take and takeLast. It's also true for any curried function like this where the generic is set within the return type

For whatever reason, when you have a return type that is more than a single possibility, generics on those get immediately evaluated. I have a whole write up on it here: #54

The flip side of your fix is you lose support for the overload xs: string. However, I've been leaning more and more towards removing the extra overloads as my discussion suggests

Here are other alternatives that will solve you problem in the mean time: https://tsplay.dev/m0Voxw

@TClark1011
Copy link
Author

TClark1011 commented Oct 13, 2023

My solution can still accommodate the string overload, we just have to add another signature for it, so the new declaration would look like this:

export function take<T>(n: number, xs: readonly T[]): T[];
export function take<T>(n:number): (xs: readonly T[]) => T[];
export function take(n: number, xs: string): string;
export function take(n:number): (xs: string) => string;

@TClark1011
Copy link
Author

TClark1011 commented Oct 13, 2023

Okay I actually figured out a workaround that can handle both the string and array curried signature and allow the generic to be evaluated after the original call: https://tsplay.dev/N7QPGW

I understand this may not be the ideal solution, it was just an idea I had and thought it was worth mentioning.

@Harris-Miller
Copy link
Collaborator

@TClark1011 that's a fantastic solution for the issue with the generic. It won't solve the problem for all the functions that share this issue, but works for take and takeLast

#70

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

2 participants