Common Event Handler Type Errors and Fixes
You'll learn how to diagnose and fix the most frequent TypeScript errors when typing event handlers in React.
The Core Problems
TypeScript will complain when your event handler types don't match React's expectations. The most common errors fall into three categories:
1. Native DOM vs React Synthetic Events
// ❌ Wrong: Using native DOM Event type
const handleClick = (e: MouseEvent) => {
e.preventDefault(); // May work, but type is wrong
};
// ✅ Correct: Using React's synthetic event
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault(); // Properly typed
};
The fix: Always use React.MouseEvent, React.ChangeEvent, etc., not the browser's native MouseEvent or Event.
2. Generic Parameter Mismatch
// ❌ Wrong: Generic doesn't match actual element
const handleClick = (e: React.MouseEvent<HTMLInputElement>) => {
// Using button handler with input type
};
<button onClick={handleClick} />; // Type error!
// ✅ Correct: Match the element
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {};
3. Return Type Confusion
// ❌ Wrong: Returning a value from void handler
const handleClick = (e: React.MouseEvent): string => {
return "clicked"; // onClick expects void
};
// ✅ Correct: Event handlers return void
const handleClick = (e: React.MouseEvent): void => {
console.log("clicked");
};
Quick Diagnostic Checklist
- "Type 'MouseEvent' is not assignable" → Use
React.MouseEvent - "HTMLElement not assignable to HTMLButtonElement" → Fix the generic parameter
- "Type 'string' is not assignable to 'void'" → Remove the return value
Key Takeaway: React event handlers must use React's synthetic event types (not native DOM types), match the specific HTML element in generics, and return void—mismatches in any of these three areas cause type errors.