question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript types for onInput, onBlur etc..

See original GitHub issue

I 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:open
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
38elementscommented, Feb 4, 2020

This works.

import { h, render, Component, JSX } from 'preact'


class Foo extends Component {
  onInput({currentTarget}: JSX.TargetedEvent<HTMLInputElement, Event>) {
    console.log(currentTarget.value)
  }
  render() {
    return <input onInput={this.onInput} />
  }
}

render(<Foo />, document.body)
7reactions
drackp2mcommented, Dec 3, 2020

I currently do something like the following:

import { h, render, Component } from 'preact';

class Foo extends Component {
  onInput(e: Event) {
    if (e.target instanceof HTMLInputElement) {
      console.log(e.target.value);
    }
  }

  render() {
    return h('input', { onInput: this.onInput });
  }
}

render(<Foo />, document.body);

As explained in this section of the documentation: https://preactjs.com/guide/v10/typescript#typing-events

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found