Categories
TypeScript Answers

How to add types for functions with variable argument counts with TypeScript?

Spread the love

Sometimes, we want to add types for functions with variable argument counts with TypeScript.

In this article, we’ll look at how to add types for functions with variable argument counts with TypeScript.

How to add types for functions with variable argument counts with TypeScript?

To add types for functions with variable argument counts with TypeScript, we can set it the parameter variable to an array type.

For instance, we write

interface Example {
  func(...args: any[]): void;
}

to set the data type of args to any[] since args is an array that has all the arguments that we call func with.

Conclusion

To add types for functions with variable argument counts with TypeScript, we can set it the parameter variable to an array type.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *