Skip to content
Advertisement

How to get “Element” -type value from “Element.parentNode” instead of “Node & ParentNode”?

The recipe of event delegating from http://youmightnotneedjquery.com/:

JavaScript

I am trying rewrite it to TypeScript type-safely (for click event for now):

JavaScript

TypeScript compiler tells me:

JavaScript

The .matches() is the method of the Element – I can’t to call it from Node & ParentNode). What have I do?

If targetParentNode = targetParentNode.parentNode as Element is completely all right please explain why.

P. S. Please note that any, object and type annotation omitting are not allowed.

Advertisement

Answer

You just need to cast targetParentNode.parentNode to Element.

So for loop might be look like this:

JavaScript

PlaygrounLink

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement