0

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) => {...}
1

1 Answer 1

2

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) => {}
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any way to do this without having to specify types in an array?
I think no. Even the suggested solution is an workaround.
What would you prefer instead of the tuple?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.