Amsvartner
Amsvartner

Reputation: 793

TypeScript - type of function with variable number of parameters

I would like to create a type called OnClick that accepts a variable number of types as arguments. How would I go about to make this work? I want to be able to use parameters of different types.

type OnClick<..> = (..) => void;

// usage:
const onClick: OnClick<string, number> = (key, value) => {...}
const onClick2: OnClick<SomeType, AnotherType> = (someThing, anotherThing) => {...}

Upvotes: 0

Views: 437

Answers (1)

You can use constraint to T to declare it as tuple and then use it to type args:

type OnClick<T extends any[]> = (...args: T) => void;

// usage:
const onClick: OnClick<[string, number]> = (key, value) => {}

Upvotes: 2

Related Questions