title: React TypeScript type for mousedown and touchstart events"
body: <p>How can I specify a TypeScript type for <code>mousedown</code> and <code>touchstart</code> events?</p>
  
  <pre><code>function useOnClickOutside(ref: React.RefObject&lt;any&gt;, handler: () =&gt; void) {
    React.useEffect(() =&gt; {
      const listener = (event: React.TouchEvent | React.MouseEvent) =&gt; {
        if (!ref || !ref.current) {
          return;
        }
        const elem = document.getElementsByClassName(uniqueClassName)[0];
        if (elem.contains(event.target)) {
          return;
        }
        handler();
      };
  
      document.addEventListener("mousedown", listener);
      document.addEventListener("touchstart", listener);
  
      return () =&gt; {
        document.removeEventListener("mousedown", listener);
        document.removeEventListener("touchstart", listener);
      };
    }, [ref, handler]);
  }
  </code></pre>