Checking the type of an element via element.tagName === 'name'
doesn't give you type guard and you have to use optional chain (or !
a.k.a. Typescript's non-null assertion operator) if the element can be undefined
or null
.
Some elements have a corresponding JavaScript interface (e.g. HTMLImageElement
for <img>
), one can also use instanceof
to check the element type without the aforementioned shortcomings.
An real life example is: 574d442
(#5036)
if (event.target.parentElement?.tagName === 'img') {
return (target as HTMLImageElement).src
}
if (event.target.parentElement instanceof HTMLImageElement) {
return target.src
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too