Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript rejects an onClick handler because the type is void

ErrorType 'void' is not assignable to type '((event: MouseEvent<HTMLInputElement>) => void) | undefined'

Short answer

TypeScript says 'void' because your JSX has the function call in it, not the function. onClick={handle()} runs during render and gives the prop its result, which is void. Pass the function (onClick={handle}) or wrap it in an arrow function (onClick={() => handle(id)}), and give extracted handlers an event type.

  • Applies to TypeScript 5.x, React 19, @types/react 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the line the error points at and look at the event prop. If the value ends in a pair of round brackets, the function is being called there.

    grep -rnE "on[A-Z][A-Za-z]+=\{[A-Za-z.]+\([^)]*\)\}" src/
  2. 2Match what you see, then jump to its fix

    1. 1onClick={handleClick()} or onClick={doSomething(id)}The handler is called in the JSX instead of passedFix
    2. 2The handler is declared as a separate function and TypeScript complains at the prop or at the parameterAn extracted handler has no event type, or the wrong oneFix

What does the error look like?

  • Type 'void' is not assignable to type '((event: MouseEvent<HTMLInputElement>) => void) | undefined'

Cause 1 of 2

The handler is called in the JSX instead of passed

1Check

In JSX, code inside the braces runs right away during rendering. The function runs on every render, not on click, and the prop receives its return value. A function that returns nothing gives the prop the type void.

grep -rn "onClick={[a-zA-Z]*(" src/

2Fix

Pass the function without brackets. When you need to give it an argument, wrap the call in an arrow function so it runs only on click.

<button onClick={handleClick}>Save</button>
<button onClick={() => handleDelete(item.id)}>Delete</button>

3Verify

The type error is gone and the function no longer runs when the page renders.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 2 of 2

An extracted handler has no event type, or the wrong one

1Check

TypeScript infers the event type for an inline handler. When you move the code to its own function, you must write the event type yourself, and it must fit the element the prop sits on.

npx tsc --noEmit --pretty

2Fix

Type the parameter with the React event type for the element, or type the whole function with the handler type from React.

import type { MouseEvent, MouseEventHandler } from 'react';

function handleClick(event: MouseEvent<HTMLInputElement>) {
  console.log(event.currentTarget.value);
}

const handleClick2: MouseEventHandler<HTMLInputElement> = (event) => {
  console.log(event.currentTarget.value);
};

3Verify

Hover the prop in the editor: it shows the handler type, and the code compiles.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Treat a pair of brackets after a handler name in JSX as a red flag in code review.
  • Use an arrow function only when you need to pass an argument.
  • Hover over the event prop in your editor to read the type TypeScript expects.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.