TypeScript-DOM-lib-generator
TypeScript-DOM-lib-generator copied to clipboard
SVGRect should not alias DOMRect in lib.dom.d.ts
In the currently generated lib types, SVGRect is equivalent to DOMRect. The initial SVG recommendation of SVGRect is as an independent interface, not associated with DOMRect. While the Geometry Interfaces Module Level 1 IDL specifies SVGRect as a legacy alias for DOMRect, the SVG WG agreed to revert merging SVG and DOM geometry interfaces in 2019, keeping SVGRect as an distinct interface. The decision was made as a result of no browser supporting the change due to implementation difficulties.
The adverse effect of this with respect to lib.dom.d.ts is that TypeScript does not produce an error when accessing properties of DOMRect that do not exist on SVGRect and, thus, will fail at runtime. Additionally, editors will suggest completions for these properties that will not exist at runtime. Properties available on DOMRect but are not implemented on SVGRect include bottom
, top
, left
, and right
inherited from DOMRectReadOnly.
Additionally, some methods on SVG interfaces return DOMRect instead of SVGRect, which is problematic when they are not aliases of the same type. For example:
SVGGraphicsElement.getBBox(options?: SVGBoundingBoxOptions): DOMRect;
Since SVGRect is not actually implemented as an alias of DOMRect, these methods should return an SVGRect for proper type-checking. This likely affects other geometry interfaces that have been aliased or declared to be DOM versions when only SVG versions are supported.
I've just been hit by this myself and I've wanted to fix this here. Since the types are generated I'm unsure where this alias originates and I'm unsure how I should override this with a proper type. If I could get some guidance from the maintainers here I could prepare a PR for this change.