Skip to content

document.getElementById is not correctly typedΒ #60996

Closed as not planned
Closed as not planned
@Judekeyser

Description

@Judekeyser

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions