Skip to content

What's the TypeScript type of the handler used in an array event binding? #1376

Answered by genericGenie
jyasskin asked this question in Q&A
Discussion options

You must be logged in to vote

@jyasskin,
I just bumped into this issue as well. For the type, I specified the array parameter position to use [0] because it has 2 parameters.
I need the event and some optional custom data structure I pass into it. Below is a simplified example.

const myHandler: JSX.BoundEventHandler<HTMLDivElement, MouseEvent>[0] = (data, e): void => {
     if (Array.isArray(data))
        console.log(`tagName: ${e.target.tagName} = `, data.join(' '))
     else if (typeof data === 'object' && Object.hasOwn(data, 'myObj'))
        console.log(`tagName: ${e.target.tagName} = `, data.myObj)
}

<div onClick={ [myHandler, ['hello', 'world', 'array']] }>Click Array</div>
<div onClick={ [myHandler, {myObj: '…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jyasskin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants