TypeScript-DOM-lib-generator icon indicating copy to clipboard operation
TypeScript-DOM-lib-generator copied to clipboard

Adjust `.parentElement` type to include `SVGElement`

Open Andarist opened this issue 3 years ago • 6 comments

In reality, this also includes | null but this has been removed in https://github.com/microsoft/TypeScript-DOM-lib-generator/commit/2cd8ad3974b63affd454164028960821e77a2e17 so I guess you want to omit that for pragmatic reasons.

Originally this was typed as Element | null (and that probably was the most "correct" type) but that was changed to HTMLELement | null here: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/560 . However, SVGElement can appear here in a lot of scenarios.

An HTML element can be a parent of SVG (the simplest <div><svg/></div>), SVG element can be a parent of another SVG element (<svg><rect/></svg>) but also the SVG element can be a parent of an HTML element (<svg><foreignObject><div></div></foreignObject></svg>)

Andarist avatar Nov 10 '21 12:11 Andarist

Thanks for the PR!

This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged.

github-actions[bot] avatar Nov 10 '21 12:11 github-actions[bot]

Build has failed because you missed to include the generated files.

But did you considered the feedback from #1151 ?

HolgerJeromin avatar Nov 10 '21 12:11 HolgerJeromin

Sorry, I've missed that discussion. I see the points raised there but having to cast this through unknown/any first and then to SVGElement doesn't seem like a good DX. I understand that people might deal with SVG less often than with HTML but there should be a clear way of working with SVGs too. Not everyone might figure out how to "properly" cast this as it's not intuitive.

It also seems that there is no good list of "expectations" about the DOM types. A lot of this is gray area and the APIs don't provide full type safety. It would be great if things like this would be documented somewhere.

Andarist avatar Nov 11 '21 06:11 Andarist

Happy to have a FAQ of some sort, maybe this could be the first one?

orta avatar Nov 15 '21 16:11 orta

@orta where such a FAQ should live in? this repo? main TS docs?

Andarist avatar Nov 15 '21 16:11 Andarist

In here I think, TypeScript's FAQ is already very very long

orta avatar Nov 15 '21 16:11 orta