22

Here's the code:

function bodyClickHandler(ev: MouseEvent) {
    if(containerRef.current && containerRef.current.contains(ev.target)) return;
    setOpen(false);
}

The error:

Argument of type 'EventTarget | null' is not assignable to parameter of type 'Node | null'. Type 'EventTarget' is missing the following properties from type 'Node': baseURI, childNodes, firstChild, isConnected, and 44 more.ts(2345)

containerRef.current is a HTMLDivElement. .contains ought to be this function.

Is ev.target not guaranteed to be a Node of some sort?

I can cast it (ev.target as Node) but I'm trying to figure out in what scenario this could possibly fail? What can I click on that isn't a Node?

3
  • 1
    Though they probably don't all make sense in case of mouse events, github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts lists 55 interfaces besides Node that extend EventTarget.
    – Oblosys
    Apr 11, 2020 at 21:56
  • @Oblosys Hmm.. seems like a deficiency in lib.dom.ts; I think MouseEvent should be more specific :-( Oh well.
    – mpen
    Apr 11, 2020 at 22:01
  • I think it's tricky to come up with accurate typings for EventType with TypeScript's type system. For currentTarget it's easier, but looking at your code example it seems you indeed need target.
    – Oblosys
    Apr 11, 2020 at 22:09

2 Answers 2

28

Short answer: It's due to poor typings on their part. Go ahead and cast it.

Long answer: The reason this happens is because if you click on an <svg /> inside a button (for example), the event.target will point to the SVG, which is not guaranteed to be a button (obviously). Only an event's currentTarget points to the element the handler's on. Therefore the people who made the React typings decided that they would define and event's currentTarget as what the button was in our example, but make no guarantees about the event's target except that it is an EventTarget. Therefore you are stuck with having to cast it. This is discussed in this PR.

4
  • This isn't the synthetic MouseEvent though. I am using React, but React doesn't provide easy access to add events to document.body, so this is a native one added via document.body.addEventListener.
    – mpen
    Apr 11, 2020 at 22:07
  • 1
    @mpen It looks like they have the same feature/problem in the DOM typings. If anything, it's worse because DOM MouseEvents aren't generic. Apr 11, 2020 at 22:09
  • It is not guaranteed to be button but it is at least guaranteed to be a Node, right? Jul 29, 2021 at 21:27
  • I would think so. Jul 31, 2021 at 3:28
11

I'm just starting with Typescript. I have a similar problem, my solution is, I know that the event is always fired by a DomElement.

And this is how I inform Typescript:

function bodyClickHandler(ev: MouseEvent) {
    if(containerRef.current && containerRef.current.contains(ev.target as HTMLElement)) return;
    setOpen(false);
}

If you prefer to separate it:

function bodyClickHandler(ev: MouseEvent) {
    const target = ev.target as HTMLElement;
    if(containerRef.current && containerRef.current.contains(target)) return;
    setOpen(false);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.