Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass in an array for rest parameter?

I have a function that accepts a rest parameter.

function getByIds(...ids: string){
...
}

I know I can call getByIds('andrew') and getByIds('andrew','jackson') and it will convert the strings into an array of strings.

My question is: can I call getByIds(['andrew','jackson']), if I already have the parameters merged into an array?

In Java I know I could, but typescript seems to give me problems. JsFiddle fails me too.

like image 219
f.khantsis Avatar asked May 10 '17 16:05

f.khantsis


1 Answers

Yeah, you can use the spread operator:

getByIds(...['andrew', 'jackson']);

It compiles to:

getByIds.apply(void 0, ['andrew', 'jackson']);
like image 57
Nitzan Tomer Avatar answered Sep 20 '22 18:09

Nitzan Tomer