How to pass an array as arguments in TypeScript?

Sometimes, we want to pass an array as arguments in TypeScript.

In this article, we’ll look at how to pass an array as arguments in TypeScript.

How to pass an array as arguments in TypeScript?

To pass an array as arguments in TypeScript, we can rest the rest synax.

For instance, we write

const m1 = (...args: any[]) => {
  //...
};

const m2 = (str: string, ...args: any[]) => {
  m1(...args);
};

to create the m1 function that takes an unlimited number of arguments.

And we get the arguments from the args array.

Then in m2, we call m1 with the args entries spread in the parentheses as arguments.

Conclusion

To pass an array as arguments in TypeScript, we can rest the rest synax.