1

Is it possible to type a DOM element in TypeScript? If it is how do we do? For example, can we do:

var myDocument: any = document;
John Weisz
  • 30,137
  • 13
  • 89
  • 132
J. Bran
  • 11
  • 1
  • 3
  • Take a look at the answers to this [question](http://stackoverflow.com/q/14742194/398606). You can use `HTMLElement` or the type can be inferred in some cases. – Sunil D. Apr 13 '17 at 15:56

1 Answers1

2

tl;dr; Just use document in your code.

The types of all DOM nodes are already defined in the TypeScript compiler. See the documentation on the compiler option --lib about the value DOM.

Notice: You shouldn't do let myDocument: any = document. The : any would lose the typing. You can do: let myDocument = document and the type of myDocument will be inferred from the type of document.

Paleo
  • 21,831
  • 4
  • 65
  • 76