title: React TypeScript: Type 'null' is not assignable to type 'CanvasRenderingContext2D'
body: <p>I seem to be able to create the canvas but when I come to get the context</p>
  
  <pre><code>  const resizeImage = async (maxSize: number) =&gt; {
      const image = originalImage;
      const id = positionId;
      const resizeCanvas = document.createElement('canvas');
      resizeCanvas.width = maxSize;
      resizeCanvas.height = maxSize;
      const ctx: CanvasRenderingContext2D = resizeCanvas.getContext('2D');
  //         ^ Error
      ...
  </code></pre>
  
  <p>The error is </p>
  
  <pre><code>Type 'CanvasRenderingContext2D | ImageBitmapRenderingContext | WebGLRenderingContext | WebGL2RenderingContext | null' is not assignable to type 'CanvasRenderingContext2D'.
    Type 'null' is not assignable to type 'CanvasRenderingContext2D'.ts(2322)
  </code></pre>