Skip to content

EventTarget does not extend HTMLElement #29540

@maximilianMairinger

Description

@maximilianMairinger

TypeScript Version: 3.2.4

Search Terms:

event.target instanceof HTMLElement eventTarget not instanceof HTMLElement

Code

// event.target is instanceof HTMLElement
document.querySelector("div").addEventListener("click", (event) => {
    // shows error: "EventTarget is missing the following properties from HTMLElement ..."
    let t: HTMLElement = event.target;

    // Though:
    console.log(event.target instanceof HTMLElement) //true
})

Expected behavior:

The interface EventTarget should extend HTMLElement or event.target should return HTMLElement

Actual behavior:

EventTarget is missing the following properties from HTMLElement ...

Playground Link:

https://www.typescriptlang.org/play/index.html#src=%2F%2FNot%20relevant%0D%0Alet%20div%20%3D%20document.createElement(%22div%22)%3B%0D%0Adiv.style.width%20%3D%20%22100px%22%3B%0D%0Adiv.style.height%20%3D%20%22100px%22%3B%0D%0Adiv.style.background%20%3D%20%22red%22%3B%0D%0Adocument.querySelector(%22body%22).append(div)%3B%0D%0A%0D%0A%2F%2Frelevant%0D%0Adocument.querySelector(%22div%22).addEventListener(%22click%22%2C%20(e)%20%3D%3E%20%7B%0D%0A%09let%20t%3A%20HTMLElement%20%3D%20e.target%3B%0D%0A%0D%0A%09console.log(e.target%20instanceof%20HTMLElement)%3B%0D%0A%7D)

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions