microsoft / microsoft/TypeScript

The type of Document.documentElement could be SVGSVGElement

Open
#29,052 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Revisit Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at lib/lib.dom.d.ts, the linked declaration for Document.documentElement, and reproduce the DOMParser SVG example. Done means the DOM typings accurately admit the demonstrated SVG documentElement and the issue’s SVGDocument question is resolved, with the relevant declaration checks passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.