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

feat(curry): enhance type #533

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat(curry): enhance type #533

wants to merge 1 commit into from

Conversation

Plumbiu
Copy link

@Plumbiu Plumbiu commented Sep 15, 2024

I noticed that the type tag of the curry function is enumerated. When the number of parameters exceeds a certain value, the type will be any.

There are three cases for currying function types:

  1. no parameter, return () => R
  2. one parameter, return (p: P) => R
  3. More than one parameter, return (p: P) => Curried<Rest, P>
type Curried<A extends any[], R> = A extends []
  // If the function parameter length is 0, return () => R
  ? () => R
  : A extends [infer P]
    // If the function parameter length is 1, return (p: P) => R
    ? (p: P) => R
    : A extends [infer P, ...infer Rest]
      // If the function has more than 1 parameter, return (p: P) => Curried<Rest, R>
      ? (p: P) => Curried<Rest, R>
      : never;
export function curry<A extends any[], R>(func: (...args: A) => R): Curried<A, R> {
  if (func.length === 0 || func.length === 1) {
    return func as Curried<A, R>;
  }

  return function (arg) {
    return makeCurry(func, func.length, [arg]);
  } as Curried<A, R>;
}

Copy link

vercel bot commented Sep 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 15, 2024 3:08am

@raon0211
Copy link
Collaborator

Hello, thanks for your suggestion!

The design principle of es-toolkit is to offer the simplest interface and implementation for 85% of major use cases. We recognize that the type you suggested covers all scenarios, but we believe that currying functions with more than 5 parameters isn’t very common for most users. That’s why we decided to stick with the current type interface.

What do you think about this?

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