Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating new HTMLElement in TypeScript

Tags:

typescript

I am trying to instantiate new HTMLDivElement in TypeScript

var elem = new HTMLDivElement(); 

but the browser throws

Uncaught TypeError: Illegal constructor. 

The workaround seems to be to use

var elem = document.createElement('div'); 

but I find this suboptimal for various reasons.

Why can't I instantiate DOM elements directly when there is a new keyword for in in the lib.d.ts?

declare var HTMLDivElement: {     prototype: HTMLDivElement;     new (): HTMLDivElement; } 
like image 304
daniel.sedlacek Avatar asked Oct 06 '14 16:10

daniel.sedlacek


People also ask

How do you access HTML elements in TypeScript?

To get or access the properties and methods of the div HTML element tag without having errors or red squiggly lines in TypeScript, you have to assert the type for the div HTML element tag using the HTMLElement interface in TypeScript.

How do you initialize an element in HTML?

The HTMLFormElement. reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method.

What is the difference between element and HTML element?

HTMLElement refers explicitly to an HTML element whereas Element may refer to an XML element. However, HTMLElement is technically a subset of Element.


2 Answers

Note that the error you get here is "Illegal constructor". This is different from the error "object is not a function" that you would get if you were to write new {}(), for example.

So, technically, HTMLDivElement does have a constructor. It's just that this particular constructor throws an exception rather than creating an object.

Normally lib.d.ts would just exclude such useless signatures, but TypeScript requires that the right-hand side of the instanceof operator have a constructor. In other words, it's legal to write foo instanceof HTMLElement, but not foo instanceof [], and the difference is determined by the fact that HTMLElement has a constructor.

There were basically three choices on the table here:

  1. Remove the construct signature from the DOM elements and remove the restriction on instanceof's right operand. This is undesirable because you'd really prefer to catch errors where someone accidentally writes code like x instanceof foo instead of x instanceof Foo (where foo is an instance of Foo).
  2. Remove the construct signature from DOM elements, but keep the instanceof check in place. This is bad because foo instanceof HTMLElement is a very reasonable thing to write.
  3. The current situation, where the constructors exist but you have to know not to call them.

You can't construct DOM elements using normal constructors because you're supposed to go through document.createElement. This is basically for technical reasons relating to how browsers implement these objects.

like image 192
Ryan Cavanaugh Avatar answered Nov 10 '22 02:11

Ryan Cavanaugh


You have mistaken typescript syntax with c# syntax.

Just replace

var elem = new HTMLDivElement(); 

with one of the following

var elem = document.createElement('div'); 

or

var elem = <HTMLDivElement>(document.createElement('div')); 

or

let elem = document.createElement('div') as HTMLDivElement 

(if you need to use HTMLDivElement attributes)

like image 38
Mostafa Khodakarami Avatar answered Nov 10 '22 02:11

Mostafa Khodakarami