Skip to content
the poor coder

Type '(e: React.ChangeEvent<HTMLInputElement>) => void' is not assignable to type 'FormEventHandler<HTMLFormElement>'. Types of parameters 'e' and 'event' are incompatible.

Poor Man
Published
Reading time
2 min read
On this page

Understanding TypeScript Error: Type '(e: React.ChangeEvent<HTMLInputElement>) => void' is not assignable to type 'FormEventHandler<HTMLFormElement>'. Types of parameters 'e' and 'event' are incompatible.#

As a web developer, I have come across this error message multiple times while working with TypeScript. This error is related to the type of event handlers in TypeScript. In this blog post, I will try to explain what this error means and how to fix it.

What's the problem?#

The error message is quite clear in stating that there is a type mismatch between two types of event handlers: '(e: React.ChangeEvent<HTMLInputElement>) => void' and 'FormEventHandler<HTMLFormElement>'.

Let's break down these two types:

  • '(e: React.ChangeEvent<HTMLInputElement>) => void': This is a type of event handler that takes an argument of type 'React.ChangeEvent<HTMLInputElement>' and returns nothing.
  • 'FormEventHandler<HTMLFormElement>': This is a type of event handler that takes an argument of type 'Event' and returns nothing. The argument is expected to be an event that occurs on a 'form' element.

The error message is stating that these two types are not compatible because the argument expected for the first type, 'React.ChangeEvent<HTMLInputElement>', is more specific than the argument expected for the second type, 'Event'.

How to fix it?#

To fix this error, we need to make sure that the argument type for our event handler matches the type expected by the 'FormEventHandler<HTMLFormElement>' type.

One way to do this is to change the type of our event handler to 'FormEventHandler<HTMLFormElement>'. Here's an example:

Text

const handleSubmit: FormEventHandler<HTMLFormElement> = (event) => {
  event.preventDefault();
  // rest of the code
}

In this code, we changed the type of our event handler 'handleSubmit' to 'FormEventHandler<HTMLFormElement>'. This ensures that our event handler function takes an argument of type 'Event' which is expected by the 'FormEventHandler' type.

Another way to fix this error is to change the type of our event handler to '(e: React.FormEvent<HTMLFormElement>) => void'. Here's an example:

Text

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
  event.preventDefault();
  // rest of the code
}

In this code, we changed the type of our event handler 'handleSubmit' to '(e: React.FormEvent<HTMLFormElement>) => void'. This ensures that our event handler function takes an argument of type 'React.FormEvent<HTMLFormElement>' which is equivalent to the 'FormEventHandler' type.

Conclusion#

TypeScript is a great tool for catching errors in our code before it even runs. In this blog post, we learned about one such error related to the type of event handlers. We also learned two ways to fix this error. By understanding and fixing these errors, we can write better and more robust code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC