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?
Node
that extendEventTarget
.EventType
with TypeScript's type system. ForcurrentTarget
it's easier, but looking at your code example it seems you indeed needtarget
.