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

Should be possible to spread Parameters onto its function #36874

Open
Svish opened this issue Feb 19, 2020 · 4 comments
Open

Should be possible to spread Parameters onto its function #36874

Svish opened this issue Feb 19, 2020 · 4 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@Svish
Copy link

Svish commented Feb 19, 2020

TypeScript Version: 3.7.5

Search Terms:
function parameters spread

Expected behavior:
Expected the playground example to compile without errors

Actual behavior:
The code works great, but Typescript throws the following error:

Type 'Parameters' must have a 'Symbol.iterator' method that returns an iterator. (2488)

Code

export function call<F extends (...args: any) => any>(
  fn: F,
  ...parameters: Parameters<F>
): ReturnType<F> {
  return fn(...parameters);
}

Playground Link: Provided

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 10, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 10, 2020
@patrickshipe
Copy link

As a workaround, ...args: [...Parameters<T>] works.

@haggen
Copy link

haggen commented Aug 31, 2020

Use (...args: any[]) => any and it'll work: Playground

On a tangent, IMO, this looks a bit classier:

function call<F extends (...args: any[]) => any, P extends any[] = Parameters<F>, R = ReturnType<F>>(
  fn: F,
  ...parameters: P
): R {
  return fn(...parameters);
}

@haggen
Copy link

haggen commented Sep 2, 2020

I'm still uncertain of how to achieve that in strict mode though.

@MartinJohns
Copy link
Contributor

As mentioned by @AlCalzone in #41728, this seems to work when replacing any with any[].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants