Skip to content

The type of Document.documentElement could be SVGSVGElement #29052

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

Open
EFanZh opened this issue Dec 16, 2018 · 4 comments
Open

The type of Document.documentElement could be SVGSVGElement #29052

EFanZh opened this issue Dec 16, 2018 · 4 comments
Labels
Revisit An issue worth coming back to Suggestion An idea for TypeScript

Comments

@EFanZh
Copy link

EFanZh commented Dec 16, 2018

Currently, the type of Document.documentElement is HTMLElement:

https://github.com/Microsoft/TypeScript/blob/4d74f67325d305f52a2b00b4f30b5a4f3210c649/lib/lib.dom.d.ts#L4124

But it is possible for a Document’s documentElement property to be of type SVGSVGElement which is not a subtype of HTMLElement:

Try to run the following code:

const d = (new DOMParser()).parseFromString('<svg xmlns="http://www.w3.org/2000/svg"></svg>', 'image/svg+xml');
const k = (d instanceof Document) && (d.documentElement instanceof SVGSVGElement);

the value of k should be true.

Also, why isn’t there a SVGDocument in TypeScript?

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Dec 16, 2018

Probably because it's usually used with HTML and not anything else that isn't HTML. And HTML should have the <html> tag as the root element.

You can pretty much have any XML tag be the documentElement.

Might be oversight. Might be deliberate after weighing correctness vs. ease of the "usual" use case

@nmain
Copy link

nmain commented Dec 17, 2018

This can probably be subsumed by #4689. Although originally about parentElement, discussion in that ticket includes documentElement as well.

@weswigham
Copy link
Member

AFAIK we know this and override the Element base type with HTMLElement because most usages are with the DOM's document.

@AGluk
Copy link

AGluk commented Sep 8, 2023

I think that documentElement should be just Element.
Then it can be used as HTMLElement or SVGSVGElement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Revisit An issue worth coming back to Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants