React: Add getBoundingClientRect to EventTarget type

Created on 25 Jul 2019  ·  2Comments  ·  Source: facebook/react

Hi.
I've found that I can't use getBoundingClientRect with MouseEvent in typescript.

onClick: (event: MouseEvent): void => {
  const { left, top } = event.target.getBoundingClientRect();
  const { left, top } = event.nativeEvent.target.getBoundingClientRect();
},

Typescript message I see in VS code is:

Property 'getBoundingClientRect' does not exist on type 'EventTarget'.ts(2339)

Most helpful comment

This is what worked for me, but I guess for MouseEvent it should work out of the box
const node = event.target as HTMLElement;

All 2 comments

This is what worked for me, but I guess for MouseEvent it should work out of the box
const node = event.target as HTMLElement;

this looks like an issue for the typescript bindings, the react team doesn't own those.

Was this page helpful?
0 / 5 - 0 ratings