Hello! Welcome back to our module on "Forms and Event Handling."
In the last lesson, we explored how to make components interactive by defining event handler functions and passing them to JSX props like onClick and onSubmit. We also took a look under the hood at React's SyntheticEvent system, which ensures consistent event behavior across all browsers.
However, if you tried the code examples from the last lesson in a strict TypeScript environment, you would have run into an error: Parameter 'event' implicitly has an 'any' type. This is TypeScript's way of telling us we've left a door open for bugs.
Today, we will close that door. This lesson is dedicated to achieving the learning outcome: to type React's synthetic events correctly in a TypeScript environment. By properly typing our event handlers, we make our code safer, easier to read, and we unlock excellent autocompletion in our IDE.
1. The Problem: Implicit any
Let's start by visualizing the problem. Imagine we have a simple form submission handler from our previous lesson:
const handleSubmit = (event) => { // <-- TypeScript error here!
event.preventDefault();
console.log('Form submitted!');
};
// ... in JSX
<form onSubmit={handleSubmit}>...</form>
When you write this, TypeScript can't be sure what event is. Is it a mouse click? A keyboard press? A form submission? To prevent potential runtime errors, it flags this as an issue.

So, how do we find the correct type for each specific event handler?
2. Typing Events: The Direct Approach
React, in conjunction with its type definitions package (@types/react), exports a comprehensive set of types for all its synthetic events. The most direct way to type your handlers is to import these types and apply them to the event parameter.
Your IDE is your most valuable tool here. A common and effective workflow is to "hover to discover":
- Write your handler inline in the JSX temporarily.
- Hover over the
eventparameter inside the inline function. - Your IDE will show you the inferred type.
- Use that type in your standalone handler function.
Let's see this in practice with the two most common event types: MouseEvent and ChangeEvent.
React events and TypeScript: a complete guide
The following article provides a clear, practical guide to typing events. It demonstrates the fundamental patterns for handling clicks and input changes, which we will build on throughout this lesson.
Please read the section titled 'Typing the event'. Pay close attention to: How MouseEvent is used for an onClick handler. How ChangeEvent<HTMLInputElement> is used for an onChange handler. The explanation of generics, and why ChangeEvent needs the extra <HTMLInputElement> information.
As the article explains, there are two key patterns:
For Mouse Events (onClick, onMouseEnter, etc.)
The event type is React.MouseEvent. You can make it more specific by providing the HTML element type as a generic argument.
import type { MouseEvent } from 'react';
// The event parameter is now correctly typed
const handleClick = (event: MouseEvent<HTMLButtonElement>) => {
console.log('Button clicked!');
// TypeScript knows 'event' has properties like .clientX, .clientY, etc.
};
// ... in JSX
<button onClick={handleClick}>Click Me</button>
For Change Events (onChange)
The event type is React.ChangeEvent. This type is generic and requires you to specify which kind of element triggered the change, as the target property differs between them.
import type { ChangeEvent } from 'react';
// For a text input
const handleInputChange = (event: ChangeEvent<HTMLInputElement>) => {
// TypeScript knows event.target is an HTMLInputElement, so .value is a string.
console.log(event.target.value);
};
// For a dropdown/select
const handleSelectChange = (event: ChangeEvent<HTMLSelectElement>) => {
// TypeScript knows event.target is an HTMLSelectElement.
console.log(event.target.value);
};
// ... in JSX
<input type="text" onChange={handleInputChange} />
<select onChange={handleSelectChange}>...</select>
3. A Complete Form Example
Let's expand on this to see how different event types work together in a single component, like a form. In addition to clicks and changes, form submissions have their own event type.

The following reading expands on what we've learned and applies it to form submission.
React events and TypeScript: a complete guide
Let's return to the 'React events and TypeScript' guide to see how form submission events are typed.
Please read the section 'Form Events'. It shows a complete example with both a select element's onChange event and the form's onSubmit event, using ChangeEvent<HTMLSelectElement> and FormEvent respectively.
Here's a summary of the most common event types you'll use:
| Event Handler Prop | Event Type Definition | Element |
|---|---|---|
onClick |
React.MouseEvent<T> |
button, div, etc. |
onChange |
React.ChangeEvent<T> |
input, select, textarea |
onSubmit |
React.FormEvent<T> |
form |
onKeyDown |
React.KeyboardEvent<T> |
input, button, etc. |
onFocus / onBlur |
React.FocusEvent<T> |
input, button, etc. |
The <T> is a placeholder for the specific HTML element type, like HTMLInputElement, HTMLButtonElement, or HTMLFormElement.
Test your understanding!
You are building a login form. Your task is to add the correct TypeScript types to the event handlers. Fill in the /* ??? */ placeholders with the appropriate types.
import { useState } from 'react';
import type { /* ??? */, /* ??? */, /* ??? */ } from 'react';
function LoginForm() {
const [email, setEmail] = useState('');
const handleEmailChange = (event: /* ??? */) => {
setEmail(event.target.value);
};
const handleSubmit = (event: /* ??? */) => {
event.preventDefault();
alert(`Logging in with: ${email}`);
};
return (
<form onSubmit={handleSubmit}>
<label>
Email:
<input type="email" value={email} onChange={handleEmailChange} />
</label>
<button type="submit">Log In</button>
</form>
);
}
Show answer
Here is the completed code with the correct types.
import { useState } from 'react';
// We need ChangeEvent for the input and FormEvent for the form submission.
import type { ChangeEvent, FormEvent } from 'react';
function LoginForm() {
const [email, setEmail] = useState('');
// The change event comes from an <input> element.
const handleEmailChange = (event: ChangeEvent<HTMLInputElement>) => {
setEmail(event.target.value);
};
// The submit event comes from the <form> element.
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
alert(`Logging in with: ${email}`);
};
return (
<form onSubmit={handleSubmit}>
<label>
Email:
<input type="email" value={email} onChange={handleEmailChange} />
</label>
<button type="submit">Log In</button>
</form>
);
}
4. A Cleaner, More Programmatic Approach
Manually looking up and writing out event types works perfectly fine, but it can feel a bit repetitive. Given your background in software engineering, you might appreciate a more programmatic and maintainable way to derive these types.
TypeScript allows us to inspect the props of a component and extract the type we need. React provides a utility type called ComponentProps that makes this easy.
Instead of remembering that an onClick for a <button> needs React.MouseEvent<HTMLButtonElement>, you can just ask TypeScript for the type of the onClick prop on a standard button element.
The following video demonstrates this powerful technique.
TypeScript & React: Tips, Tricks, and Best Practices
This clip from Jolly Coding shows a great trick for extracting event handler types directly from an element's props using React.ComponentProps.
Watch from 04:55 to 06:49. The presenter first shows the manual method we've discussed, then introduces the cleaner method with React.ComponentProps<'button'>['onClick'].
Let's break down the syntax: React.ComponentProps<'button'>['onClick'].
React.ComponentProps<'button'>: This gets an object containing all the valid props for a native<button>element.['onClick']: This is an indexed access type, which plucks the type of theonClickproperty from that props object.
This pattern is especially useful when defining props for your own custom components that need to accept event handlers.
import type { ComponentProps } from 'react';
type CustomButtonProps = {
label: string;
// This type is now perfectly derived from the native button's onClick prop.
// It resolves to: React.MouseEventHandler<HTMLButtonElement> | undefined
onClick: ComponentProps<'button'>['onClick'];
};
const CustomButton = ({ label, onClick }: CustomButtonProps) => {
return <button onClick={onClick}>{label}</button>;
};
This approach is more robust because if React's underlying types were to ever change, your type would update automatically. It states your intent more clearly: "this onClick should be compatible with a standard button's onClick."
Conclusion
You now have a solid understanding of how to make your React event handlers type-safe. This practice is fundamental to writing professional, production-ready React applications with TypeScript.
Key Takeaways:
- Always provide explicit types for event handler parameters to avoid the
implicitly has an 'any' typeerror and ensure type safety. - React exports specific synthetic event types like
MouseEvent,ChangeEvent,FormEvent, andKeyboardEvent. - You can discover the correct type by hovering over an inline handler in your IDE.
- Generic event types like
ChangeEventmust be provided with the element type (e.g.,ChangeEvent<HTMLInputElement>) for full type safety onevent.target. - For a more advanced and maintainable pattern, you can use
React.ComponentProps<'element'>['onEventType']to programmatically extract the exact handler type.
In our next lesson, we will put this knowledge to immediate use. We'll explore how to implement controlled components to manage the state of form inputs. You'll see how state, events, and type safety all come together to create interactive and predictable forms.