How to avoid typescript error: Property 'innerHTML' does not exist on type 'Element'

Use a type assertion to placate the compiler:

let myContainer = <HTMLElement> document.querySelector("#myDiv");
myContainer.innerHTML = '<h1>Test</h1>';

In future versions of TypeScript, it should not be necessary to do the casting. I've sent a pull request to fix the issue:

  • error: Property 'innnerHTML' does not exist on type 'Element' (https://github.com/Microsoft/TypeScript/issues/5754)
  • https://github.com/Microsoft/TSJS-lib-generator/pull/35 (merged already)

I had the same issue, solved it by declaring it as:

myString = 'hello world';

var el: HTMLElement = document.getElementById('id_of_element');
el.innerHTML = myString;

Tags:

Typescript