Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no DOMRect type in TypeScript?

Tags:

typescript

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'?

like image 721
dukeluke Avatar asked Dec 13 '17 00:12

dukeluke


1 Answers

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.

like image 117
dukeluke Avatar answered Sep 28 '22 18:09

dukeluke