I have this situation in my typescript code:
const first = (value: any[]) => {
}
const test = (...args: any[]) => first(...args);
In function test
I pass all params in first
function. first(...args)
.
Doing this I get a typescript error: A spread argument must either have a tuple type or be passed to a rest parameter.(2556)
.
I notice many answers on this topic but they don't solve my issue.
How to solve this issue?
The error means that you need to use an Array and spread arg within it:
const first = (value: any[]) => {};
const test = (...args: any[]) => first([...args]);
Playground Link
Your first()
function expects to get an array/tuple.
So you can simply pass the array.
const first = (value: any[]) => {};
const test = (...args: any[]) => first(args);
Playground
or you can make the first
method accept variable arguments:
const first = (...value: any[]) => {};
const test = (...args: any[]) => first(...args);
Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With