Reputation: 793
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
Reputation: 1337
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