kamilkp
kamilkp

Reputation: 9800

Typescript supplied parameters do not match any signature of call target

So I have a function like this:

function foo(a, b, c, d, e) {
    // something creative
}

When I call it like this:

foo(1, 2, 3, 4, 5);

It works fine.

...But when I call it like this:

const lastTwo = [4, 5];
foo(1, 2, 3, ...lastTwo);

Typescript screams that:

error TS2346: Supplied parameters do not match any signature of call target.

How can I overcome it?

Upvotes: 1

Views: 1585

Answers (2)

Martin
Martin

Reputation: 16292

The reason is that your method signature has a a specific number of arguments, yet you are calling it with a variable number of arguments. You can solve this by changing the signature.

function foo(a, b, c, ...remaining) {
    // something creative
}

const lastTwo = [4, 5];
foo(1, 2, 3, ...lastTwo);

Upvotes: 1

Nitzan Tomer
Nitzan Tomer

Reputation: 164139

You'll need to use the apply function, but you'll also need to have all the params in the array:

const args = [1, 2, 3, 4, 5];
foo.apply(null, args);

Upvotes: 1

Related Questions