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

splitEvery typings are wrong #133

Closed
codan84 opened this issue Dec 3, 2024 · 1 comment
Closed

splitEvery typings are wrong #133

codan84 opened this issue Dec 3, 2024 · 1 comment

Comments

@codan84
Copy link

codan84 commented Dec 3, 2024

splitEvery takes 2 arguments and is curried so the following is possible:

splitEvery(2, [1,2,3,4,5,6])

splitEvery(2)([1,2,3,4,5,6])

However, your types are messed up making it hard to work with in TS:

export function splitEvery(a: number): {
  (list: string): string[];
  <T>(list: readonly T[]): T[][];
};
export function splitEvery(a: number, list: string): string[];
export function splitEvery<T>(a: number, list: readonly T[]): T[][];

Should be corrected to:

export function splitEvery(a: number, list: string): string[];
export function splitEvery(a: number): (list: string) => string[]

export function splitEvery<T>(a: number, list: readonly T[]): T[][];
export function splitEvery<T>(a: number): (list: readonly T[]) => T[][];
@kedashoe kedashoe transferred this issue from ramda/ramda Dec 4, 2024
@Harris-Miller
Copy link
Collaborator

export function splitEvery(a: number, list: string): string[];
export function splitEvery(a: number): (list: string) => string[]

export function splitEvery<T>(a: number, list: readonly T[]): T[][];
export function splitEvery<T>(a: number): (list: readonly T[]) => T[][];

This won't work for a few reasons.

First. Typescript expects the most specific first and the most generic last. The generic last is for general fall back and because that is the typedef that gets passed if you pass the function as an argument

Second, typescript tries to match first defined. This means that it will find export function splitEvery(a: number): (list: string) => string[] first, and splitEvery(2)([1,2,3,4,5,6]) will never be allowed. Because [1,2,3,4,5,6]. Typescript will not go back and look for the other def export function splitEvery<T>(a: number): (list: readonly T[]) => T[][]; to match what you're trying to give it

This is why we define it like this:

export function splitEvery(a: number): {
  (list: string): string[];
  <T>(list: readonly T[]): T[][];
};

This is says that splitEvery(2) returns a function that can accept either a string or T[] as it's arg, return the correct return type depending.

The current type definition is correct as is. Closing

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