title: IntersectionObserver API and TypeScript
body: <p>Tried to use IntersectionObserver API with Typescript, its the list with infinity scrolling.
  It works without TS, but got TS errors. I'm new in TS, And don't know how to fix it.</p>
  <pre><code>const MyItemsList: React.FC&lt;ItemsListProps&gt; = (props) =&gt; {
    const [page, setPage] = useState(0)
    const [size, setSIze] = useState(5)
  
    const observer = useRef()
    const { isLoading, content, hasMore } = useGetContent(page, size, type)
  
    const lastItemRef = useCallback((node) =&gt; {
      if (isLoading) return
      if (observer.current) observer.current.disconnect() 
      // Error: TS2532: Object is possibly 'undefined'.
      // why it should try to use `observer.current.disconnect()` after if(observer.current) condition
  
      observer.current = new IntersectionObserver(entries =&gt; { 
      //Error: TS2322: Type 'IntersectionObserver' is not assignable to type 'undefined'.
        if (entries[0].isIntersecting &amp;&amp; hasMore) {
          setPage(prevPage =&gt; prevPage + 1)
        }
      })
  
      if (node) observer.current.observe(node) 
      // Error TS2532: Object is possibly 'undefined'.
    }, [isLoading, hasMore])
  
    const itemsList = () =&gt; {
      if (typeof (content) !== 'undefined') {
        return content.map((item, index) =&gt; {
          if (content.length === index + 1) {
            return (
              &lt;li ref={lastItemRef} key={item.id}&gt;
                &lt;ItemPreview item={item} /&gt;
              &lt;/li&gt;
            )
          }
  
          return (
            &lt;li key={item.id}&gt;
              &lt;ItemPreview item={item} /&gt;
            &lt;/li&gt;
          )
  
        })
      }
      return ( &lt;div&gt;Loading...&lt;/div&gt; )
    }
    return ( &lt;ul&gt;{itemsList()}&lt;/ul&gt; )
  }
  
  export default MyItemsList
  </code></pre>
  <p>Any advice how to fix TS errors?</p>