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

[answer - typescript/overArgs] #21

Open
tyankatsu0105 opened this issue Jul 4, 2022 · 0 comments
Open

[answer - typescript/overArgs] #21

tyankatsu0105 opened this issue Jul 4, 2022 · 0 comments

Comments

@tyankatsu0105
Copy link
Collaborator

Input your answer code

type OverArgsFn<Transforms extends ((...args: any[]) => any)[]> = (
  ...args: ReturnType<Transforms[number]>[]
) => any;
export const overArgs = <Transforms extends ((...args: any) => any)[]>(
  fn: OverArgsFn<Transforms>,
  transforms: Transforms
) => {
  return (...transformsArgs: Parameters<Transforms[number]>[number][]) => {
    const transformsArgsAcc = [...transformsArgs];

    const transformsResult = transforms.map(transform => {
      const args: typeof transformsArgsAcc = [];

      for (let index = 0; index < transform.length; index++) {
        const target = transformsArgsAcc.shift();
        if (target === undefined) return;

        args.push(target);
      }

      return transform(...args);
    });

    return fn(...transformsResult);
  };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant