Tsabary
Tsabary

Reputation: 3928

How do I create an interface defining a function, that allows for any number of variables in TypeScript?

I'm using an object, that holds unlimited number of functions. To define the type of this object I've create a new interface as follows:

export interface Functions {
  [key: string]: () => void;
}

Then I had some functions that would take a variable and I got an error, because this signature interface is for a function that takes no variables, so I've changed it to this:

export interface Functions {
  [key: string]: (variables?: any) => void;
}

Which worked, but now I have a function that would take 2 variables. I can just do this:

export interface Functions {
  [key: string]: (variableOne?: any, variableTwo?: any) => void;
}

But that doesn't feel like the correct way to go about solving this. How can I just define it as a function that may take an unlimited amount of variables, or none at all.

Upvotes: 1

Views: 23

Answers (1)

Paleo
Paleo

Reputation: 23682

This syntax?

export interface Functions {
  [key: string]: (...variables: any[]) => void;
}

Upvotes: 1

Related Questions