I am attempting to determine the type of the object returned by element.getBoundingClientRect()
, which MDN lists as "DOMRect".
If I log the object to the console, it's listed as a DOMRect
with properties: height, width, top, bottom, left, right, x, and y. However, DOMRect
doesn't exist as a type in TypeScript, from what I can see.
The closest type I could find in TypeScript is ClientRect
, which is basically the same but lacks the 'x' and 'y' properties.
I also found type DOMRectInit
, which is a DOMRect
without top/bottom/left/right.
The problem I had that prompted this question was resolved when I realized that 'x' and 'y' pretty much always mirror the 'left' and 'top' properties, but what's going on here? Why is TypeScript not aligned with the standard browser API?
Also, is it safe to assume that 'x' always equals 'left' and 'y' always equals 'top'?
I found the answer to my second question in the MDN documentation:
[Left] has the same value as x, or x + width if width is negative
[Top] has the same value as y, or y + height if height is negative
So basically, it is safe to assume 'x' is the same as 'left' unless 'width' can be negative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With