1

This is the narrowed down code from my actual use case.

function wrapMe<F extends (...args: any) => any>(
    f: F,
): (...args: Parameters<F>) => ReturnType<F> {
    return function(...args: Parameters<F>): ReturnType<F> {
        return f(...args);
        //          ^^^^
        // Type 'Parameters<F>' must have a '[Symbol.iterator]()' method that returns an iterator. [2488]
    }
}

Why is this a typescript error?

1 Answer 1

1

This is a known bug in TypeScript, as described in microsoft/TypeScript#36874. Until and unless it's resolved, you can work around it either by changing the constraint to make the parameter any[] instead of any:

function wrapMe<F extends (...args: any[]) => any>(
  // -----------------------------> ^^^^^
    f: F,
): (...args: Parameters<F>) => ReturnType<F> {
    return function (...args: Parameters<F>): ReturnType<F> {
        return f(...args);
    }
}

or by wrapping Parameters<T> in a variadic tuple:

function wrapMe<F extends (...args: any) => any>(
    f: F,
): (...args: Parameters<F>) => ReturnType<F> {
    return function (...args: [...Parameters<F>]): ReturnType<F> {
    // ---------------------> ^^^^^^^^^^^^^^^^^^
        return f(...args);
    }
}

Playground link to code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.