title: Remove | null from Typescript type
body: <p>I just started learning TypeScript and in some cases I'm getting an that could be Type or null. Is there an elegant way to deal with these cases?</p>
  <pre><code>function useHTMLElement(item: HTMLElement) {
    console.log(&quot;it worked!&quot;)
  }
  
  let myCanvas = document.getElementById('point_file');
  if (myCanvas == null) {
    // abort or do something to make it non-null
  }
  // now I know myCanvas is not null. But the type is still `HTMLElement | null`
  // I want to pass it to functions that only accept HTMLElement.
  // is there a good way to tell TypeScript that it's not null anymore?
  useHTMLElement(myCanvas);
  </code></pre>
  <p>I wrote the following function that seems to work, but this seems like such a common case that I'd like to know if the language itself provides something for this.</p>
  <pre><code>function ensureNonNull &lt;T&gt; (item: T | null) : T {
    if (item == null) {
      throw new Error(&quot;It's dead Jim!&quot;)
    }
    // cast it
    return &lt;T&gt; item;
  }
  useHTMLElement(ensureNonNull(myCanvas));
  </code></pre>