I have below code snippet in my typescript file (React-TypeScript). But while running my app I'm getting error "object is possibly null" even after having null check
Error is thrown for last part of the if condition
anchorRef.current.contains(...)
const anchorRef = React.useRef(null);
const handleClose = (event: React.MouseEvent<Document, MouseEvent> | React.SyntheticEvent) => {
if (anchorRef !== null && anchorRef.current !== null && anchorRef.current.contains(event.target)) {
return;
}
setOpen(false);
};
I recently learned TypeScript so if anyone can highlight the missing piece here then that would be great.
Thnx,
Sudhir

