Property 'value' does not exist on type EventTarget in TypeScript

后端 未结 8 1028
野趣味
野趣味 2020-11-29 17:06

So the following code is in Angular 4 and I can\'t figure out why it doesn\'t work the way as expected.

Here is a snippet of my handler:

onUpdatingSe         


        
相关标签:
8条回答
  • 2020-11-29 18:04

    Passing HTMLInputElement as a generic to the event type should work too:

    onUpdatingServerName(event: React.ChangeEvent<HTMLInputElement>) {
      console.log(event);
      this.newserverName = event.target.value;
    }
    
    0 讨论(0)
  • 2020-11-29 18:04

    I was looking for a solution to a similar TypeScript error with React:

    Property 'dataset' does not exist on type EventTarget in TypeScript

    I wanted to get to event.target.dataset of a clicked button element in React:

    <button
      onClick={onClickHandler}
      data-index="4"
      data-name="Foo Bar"
    >
      Delete Candidate
    </button>
    

    Here is how I was able to get the dataset value to "exist" via TypeScript:

    const onClickHandler = (event: React.MouseEvent<HTMLButtonElement>) => {
      const { name, index } = (event.target as HTMLButtonElement).dataset
      console.log({ name, index })
      // do stuff with name and index…
    }
    
    0 讨论(0)
提交回复
热议问题