Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document.getElementById is not correctly typed #60996

Open
Judekeyser opened this issue Jan 18, 2025 · 3 comments
Open

document.getElementById is not correctly typed #60996

Judekeyser opened this issue Jan 18, 2025 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Judekeyser
Copy link

Judekeyser commented Jan 18, 2025

πŸ”Ž Search Terms

getElementById

πŸ•— Version & Regression Information

TypeScript 5.7.2

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAUwBQA8BciDOUBOMYA5gJTYASAKgLIAyAogDbIC2yYUiAPomCEyaIA3onzIoIfEgAmcCCHacAdMQnM2HKACEAngEkZGUgG5EAXyA

πŸ’» Code

No response

πŸ™ Actual behavior

The getElementById interface looks wrongly typed, as TypeScript ensures it would return a HTMLElement (see for example https://www.typescriptlang.org/docs/handbook/dom-manipulation.html#documentgetelementbyid)

However, MDN and the specification claims this method would return a generic Element , in particular it could return a SVGElement or any other kind of element (see: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById). A quick trial in a browser obviously confirms such case is legit.

πŸ™‚ Expected behavior

The return type of getElementById should be Element | null

EDIT Apparently the issue is known since at leats 2020. Fair enough. Is it possible to have a documentation that mentions this limitation and others? It would help others so much.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

This is intentional. See: microsoft/TypeScript-DOM-lib-generator#1151 (comment)

@Judekeyser
Copy link
Author

Judekeyser commented Jan 18, 2025

Good to know... thank you

There is no plan to announce a clean-up of the API in the forthcoming 3 years (say -- since the link you mention is already 3 years old) and just give users time to migrate their code base ?

Is it maybe possible to mention that limitation in the doc or something, so that other people dont have to dig github issues (which is not really direct to dig in this issue: I couldnt find any of the links mentioned above) ?

@ehoogeveen-medweb
Copy link

Might be worth filing at https://github.com/uhyo/better-typescript-lib as it seems like the sort of change they would take.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants