Why is Event.target not Element in Typescript?

后端 未结 8 692
情歌与酒
情歌与酒 2020-12-01 03:55

I simply want to do this with my KeyboardEvent

var tag = evt.target.tagName.toLowerCase();

While Event.target is of type EventTarget it doe

相关标签:
8条回答
  • 2020-12-01 04:46

    @Bangonkali provide the right answer, but this syntax seems more readable and just nicer to me:

    eventChange($event: KeyboardEvent): void {
        (<HTMLInputElement>$event.target).value;
    }
    
    0 讨论(0)
  • 2020-12-01 04:50

    Could you create your own generic interface that extends Event. Something like this?

    interface DOMEvent<T extends EventTarget> extends Event {
      target: T
    }
    

    Then you can use it like:

    handleChange(event: DOMEvent<HTMLInputElement>) {
      this.setState({ value: event.target.value });
    }
    
    0 讨论(0)
提交回复
热议问题