Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring an HTMLElement Typescript

Tags:

typescript

In the default TypeScript HTML app from visual studio, I added

HTMLElement  

to the first line of the window.onload event handler, thinking that I could provide a type for "el".

thus:

class Greeter {     element: HTMLElement;     span: HTMLElement;     timerToken: number;      constructor (element: HTMLElement) {          this.element = element;         this.element.innerText += "The time is: ";         this.span = document.createElement('span');         this.element.appendChild(this.span);         this.span.innerText = new Date().toUTCString();     }      start() {         this.timerToken = setInterval(() => this.span.innerText = new Date().toUTCString(), 500);     }      stop() {         clearTimeout(this.timerToken);     }  }  window.onload = () => {     HTMLElement el = document.getElementById('content');     var greeter = new Greeter(el);     greeter.start(); }; 

I get an error

Compile Error. See error list for details .../app.ts (25,17): Expected ';'

Any clue why? I suspect I am missing something obvious.

like image 493
bnieland Avatar asked Feb 07 '13 01:02

bnieland


People also ask

What is HTMLElement in TypeScript?

HTMLElement type According to TypeScriptLang.org, HTMLElement is “the backbone for DOM manipulation in TypeScript”. Directly from TypeScript source code: “Any HTML element. Some elements directly implement this interface, while others implement it via an interface that inherits it.”

What is HTMLElement in angular?

Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.


1 Answers

The type comes after the name in TypeScript, partly because types are optional.

So your line:

HTMLElement el = document.getElementById('content'); 

Needs to change to:

const el: HTMLElement = document.getElementById('content'); 

Back in 2013, the type HTMLElement would have been inferred from the return value of getElementById, this is still the case if you aren't using strict null checks (but you ought to be using the strict modes in TypeScript). If you are enforcing strict null checks you will find the return type of getElementById has changed from HTMLElement to HTMLElement | null. The change makes the type more correct, because you don't always find an element.

So when using type mode, you will be encouraged by the compiler to use a type assertion to ensure you found an element. Like this:

const el: HTMLElement | null = document.getElementById('content');  if (el) {   const definitelyAnElement: HTMLElement = el; } 

I have included the types to demonstrate what happens when you run the code. The interesting bit is that el has the narrower type HTMLElement within the if statement, due to you eliminating the possibility of it being null.

You can do exactly the same thing, with the same resulting types, without any type annotations. They will be inferred by the compiler, thus saving all that extra typing:

const el = document.getElementById('content');  if (el) {   const definitelyAnElement = el; } 
like image 124
Fenton Avatar answered Nov 08 '22 03:11

Fenton