Skip to content

Comments

feat(curry): enhance type#533

Closed
Plumbiu wants to merge 1 commit intotoss:mainfrom
Plumbiu:curry-type
Closed

feat(curry): enhance type#533
Plumbiu wants to merge 1 commit intotoss:mainfrom
Plumbiu:curry-type

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>;
}

@Plumbiu Plumbiu requested a review from raon0211 as a code owner September 15, 2024 03:06
@vercel
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?

@raon0211
Copy link
Collaborator

raon0211 commented Dec 1, 2024

Closing the pull request for now, feel free to open the pull request again :)

@raon0211 raon0211 closed this Dec 1, 2024
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