You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Web API type definition issue] incorrect type definition for getElementById() #2020
This API is supported in at least two major browser engines (not two Chromium-based browsers).
Have Tried The Latest Releases
This issue applies to the latest release of TypeScript.
This issue applies to the latest release of @types/web.
Additional Context
Proof:
// Run this in your console:document.body.innerHTML='<svg><g id="foo"></g></svg>'constg=document.body!.firstElementChild!.firstElementChildconsole.log(ginstanceofSVGGElement)// trueconstg2=document.getElementById('foo')console.log(g2instanceofSVGGElement)// still true (getElementById did not return HTMLElement | null)
The type is also wrong for other types of documents:
This is intentional, as most of the time the desired result would be HTMLElement. We even had complaint when ShadowRoot did return Element instead of HTMLElement. #1067
By that logic we might as well type event.target as HTMLElement instead of EventTarget which is also totally wrong. I don't think this makes a lot of sense.
I hate the current event.target type but this is far more often used on non html contexts than getElementById or parentElement:
window, document, xhr, IDBRequest, MediaQueryList...
Activity
Bashamega commentedon May 15, 2025
Can i work on this Kagami Sascha Rosylight (@saschanaz)
saschanaz commentedon May 15, 2025
This is intentional, as most of the time the desired result would be HTMLElement. We even had complaint when ShadowRoot did return Element instead of HTMLElement. #1067
trusktr commentedon May 18, 2025
By that logic we might as well type
event.targetasHTMLElementinstead ofEventTargetwhich is also totally wrong. I don't think this makes a lot of sense.HolgerJeromin commentedon May 18, 2025
I hate the current event.target type but this is far more often used on non html contexts than getElementById or parentElement:
window, document, xhr, IDBRequest, MediaQueryList...