TypeScript rejects an onClick handler because the type is void
Type '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.
Fix it fast
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/2Match what you see, then jump to its fix
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 --noEmitConfirmed by react.dev
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 --pretty2Fix
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 --noEmitConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript says no overload matches a React call
No overload matches this call.
TS2769 means your arguments fit none of the signatures of a function or JSX prop.
3 causes, each with a fixWeb apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb 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