Typescript types for onInput, onBlur etc..
See original GitHub issueI am trying to pass an Event Handler to onInput
and onBlur
but it is complaining about my types for the function itself. The code works, but I cannot find the correct type.
My event handler is:
public onInput = ({target}: {target: HTMLInputElement}) => {
const {value, name, validity} = target;
this.setInputValue(name, value);
this.setInputValidity(name, validity);
this.setInputs();
};
While Typescript expects:
Type '({ target }: { target: HTMLInputElement; }) => void' is not assignable to type 'GenericEventHandler<HTMLInputElement>'.
Types of parameters '__0' and 'event' are incompatible.
Type 'TargetedEvent<HTMLInputElement, Event>' is not assignable to type '{ target: HTMLInputElement; }'.
Types of property 'target' are incompatible.
Type 'EventTarget | null' is not assignable to type 'HTMLInputElement'.
Type 'null' is not assignable to type 'HTMLInputElement'.ts(2322)
I am passing it like this:
<input onInput={this.onInput} />
I’ve obviously tried to set it to GenericEventHandler<HTMLInputElement>
but that does not work, since I need the event passed to the function.
I am sure that I’m doing something completely wrong, but I am not sure what. I can see that in React they have SyntheticEvent
which helps with this.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Type the onFocus and onBlur events in React (TypeScript)
Use the React.FocusEvent<HTMLElement> type to type the onFocus and onBlur events in React. The FocusEvent interface is used for onFocus and onBlur events....
Read more >TypeScript definition for onBlur React event - Felix Gerschau
Full example import React, { FocusEvent } from 'react'; const InputComponent = () => { const handleFocusEvent = (e: FocusEvent<HTMLInputElement>) => { //...
Read more >React Events in TypeScript - Medium
How: Create Input Element; Create Function that will go in the onBlur attribute of the Input Element; Update the type on the created...
Read more >Typescript input onchange event.target.value - Stack Overflow
ERROR in [default] /react-onsenui.d.ts:87:18 Interface 'InputProps' incorrectly extends interface 'HTMLProps<Input>'. Types of property 'target' ...
Read more >React + TypeScript: Handling onFocus and onBlur events
The onFocus event occurs when an element or some element inside it gets focus. The onBlur event is the opposite of the onFocus...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This works.
I currently do something like the following:
As explained in this section of the documentation: https://preactjs.com/guide/v10/typescript#typing-events