Typescript: How to check tagName in eventTarget?

In Angular if tagName property is giving error on target, try to use below code

event.target['tagName']

I would adjust that code snippet like this:

var ele = <HTMLDivElement>document.getElementById("toolbar");
    ele.addEventListener("click", (ev: MouseEvent) => {
        var element = ev.target as HTMLElement;     
        if (element.tagName === "SPAN") { 
            console.log(element.tagName) 
        }  
    }, false);

Casting the event property target to HTMLElement will give us all the proper properties of the underlying element.

Check it in the playground


I found that this syntax also works fine

if ((ev.target as HTMLElement).tagName === "SPAN") {