TypeScript: type definition for a function with a variable number of parameters

In TypeScript, you can define a function with a variable number of arguments like so:

type FunctionWithArgs = (...args: string[]) => void;

If you want to restrict the number of arguments to specific values, you could also use a union type, like so (similar to Tuples in Scala):

type F1Arg = (a: string) => void;
type F2Arg = (a: string, b: string) => void;
type F3Arg = (a: string, b: string, c: string) => void;

type FunctionWithArgs2 = F1Arg | F2Arg | F3Arg;

This will let the function have 1-3 arguments – unfortunately the TypeScript compiler doesn’t seem to like this as a one liner.

Leave a Reply

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