title: Remove canvas element on click with typescript
body: <p>I am new to typescript and what i am trying is, to remove canvas element from the div. So here is my HTML code.</p>
  
  <pre><code>&lt;div class="chart"&gt;
      &lt;canvas id="nav-chart"&gt;&lt;/canvas&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>on click removing the canvas element using id <code>nav-chart</code>. So here my JS code inside the onclick function </p>
  
  <pre><code>const elem: HTMLElement = document.getElementById('nav-chart')
  if (elem) {
     elem.parentElement.removeChild(elem)
  }
  </code></pre>
  
  <p>While trying this it throws the error saying at the line <br><code>const elem: HTMLElement = document.getElementById('nav-chart')</code> <br></p>
  
  <pre><code>TS2322: Type 'HTMLElement | null' is not assignable to type 'HTMLElement'.
    Type 'null' is not assignable to type 'HTMLElement'.
  </code></pre>
  
  <p>and in this line <code>elem.parentElement.removeChild(elem)</code></p>
  
  <pre><code>TS2531: Object is possibly 'null'.
  </code></pre>