patterntypescriptCriticalCanonical
Why is Event.target not Element in Typescript?
Viewed 0 times
typescripteventwhytargetelementnot
Problem
I simply want to do this with my
While
This is probably due to some browsers not following standards, right? What is the correct browser-agnostic implementation in TypeScript?
P.S. I am using jQuery to capture the
KeyboardEventvar tag = evt.target.tagName.toLowerCase();While
Event.target is of type EventTarget, it does not inherit from Element. So I have to cast it like this:var tag = (evt.target).tagName.toLowerCase();This is probably due to some browsers not following standards, right? What is the correct browser-agnostic implementation in TypeScript?
P.S. I am using jQuery to capture the
KeyboardEvent.Solution
It doesn't inherit from
From MDN:
Element, document, and window are the most common event targets, but other objects can be event targets too, for example XMLHttpRequest, AudioNode, AudioContext, and others.
Even the
If it is an event on a DOM element, then I would say that you can safely assume
Further reading: https://github.com/Microsoft/TypeScript/issues/29540
Element because not all event targets are elements.From MDN:
Element, document, and window are the most common event targets, but other objects can be event targets too, for example XMLHttpRequest, AudioNode, AudioContext, and others.
Even the
KeyboardEvent you're trying to use can occur on a DOM element or on the window object (and theoretically on other things), so right there it wouldn't make sense for evt.target to be defined as an Element.If it is an event on a DOM element, then I would say that you can safely assume
evt.target. is an Element. I don't think this is an matter of cross-browser behavior. Merely that EventTarget is a more abstract interface than Element.Further reading: https://github.com/Microsoft/TypeScript/issues/29540
Context
Stack Overflow Q#28900077, score: 141
Revisions (0)
No revisions yet.