Remove | null from Typescript type

Viewed 2763

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?

function useHTMLElement(item: HTMLElement) {
  console.log("it worked!")
}

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);

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.

function ensureNonNull <T> (item: T | null) : T {
  if (item == null) {
    throw new Error("It's dead Jim!")
  }
  // cast it
  return <T> item;
}
useHTMLElement(ensureNonNull(myCanvas));
2 Answers
Related