H
H
hinie2022-04-09 13:32:09
typescript
hinie, 2022-04-09 13:32:09

How to cast event.target to type Node?

Hello, I have an event handler that calls a function that iterates over dom

window.addEventListener("click", (event: Event) => {
      if (!this.checkParentsHas(event.target, this.$refs.nav))
        this.isToggled = false;
    });

and a function like
checkParentsHas(el: Node, target: HTMLElement): boolean {
      let current = el;
      while (
        current.parentNode != null &&
        current.parentNode != document.documentElement
      ) {
        if (current == target) {
          return true;
        } else {
          current = current.parentNode;
        }
      }
      return false;
    },

But typescript complains about event.target
(property) Event.target: EventTarget
Returns the object to which event is dispatched (its target).

Argument of type 'EventTarget' is not assignable to parameter of type 'Node'.
  Type 'EventTarget' is missing the following properties from type 'Node': baseURI, childNodes, firstChild, isConnected, and 43 more.Vetur(2345)

How to cast event.target to type Node

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question