Clark
Clark

Reputation: 2678

Can you extend a HTMLDivElement in TypeScript?

I am making a DOM based game for the first time. I would like to extend HTMLDivElement, however in TypeScript, HTMLDivElement is an Interface.

I would like to do this pseudo class:

class QuizElement extends HTMLDivElement{

}

Sorry if this question is crazy. I am somewhat new to the DOM, and just thought, I can extend any visual class in any other environment so I guess it is do-able here!

Upvotes: 21

Views: 27246

Answers (5)

Harald
Harald

Reputation: 5113

Really late to the show, but these days yet another approach is possible. Rather than adding fields to the interface HTMLDivElement one can create a new interface like

interface QuizElement extends HTMLDivElement {
  quizScore: number;
  ...
}

and then create QuizElement objects like

const quizzi: QuizElement = Object.assign(document.createElement("div"), {
  quizScore: 0,
  ...
});

Compared to other proposed approaches, this does not "pollute" HTMLDivElement with additional fields in the scope of QuizElement.

Upvotes: 5

Kokodoko
Kokodoko

Reputation: 28128

A bit late, but apparently it's still not possible to extend HTMLDivElement. A simple way to solve extending a DIV: just use CSS to make a generic HTMLElement behave like a div.

CSS

 my-element {
    display:block;
 } 

Javascript

class MyElement extends HTMLElement{
  constructor(){
      super()
      this.innerHTML = "I behave exactly like a div"
  }
}

window.customElements.define('my-element', MyElement);

Upvotes: 3

Antonio Torres
Antonio Torres

Reputation: 342

It is not the best way but it works.

function MyElement(){
  var element = document.createElement("div");

  element.viewInner = function(){
    console.log(this, this.innerHTML)
  };

  return element;
}

var test = new MyElement();
test.innerHTML = "Hi";
test.viewInner(); // <div> Hi

Upvotes: 0

KnarfaLingus
KnarfaLingus

Reputation: 466

Also you can 'extend' the HTMLDivElement interface with data members if you wish, not by using extends since it is not a class, but by adding it via the interface. TypeScript interfaces are 'open ended', see page 85 of the spec under 'declaration merging'.

http://www.typescriptlang.org/Content/TypeScript%20Language%20Specification.pdf

for example, the below adds the member 'mydata' of type string to the HTMLDivElement interface.

interface HTMLDivElement {

    mydata : string;

}

// now we can assign a value

 var div = <HTMLDivElement>document.getElementById("myDiv"); 

 div.mydata = "test";

Upvotes: 9

Fenton
Fenton

Reputation: 250992

You can't extend HTMLDivElement because it isn't declared as a class. This makes sense, because the underlying native type doesn't make sense to extend.

You have two alternative options.

Option 1: Implements!

Because HTMLDivElement is an interface, you can implement it...

class QuizElement implements HTMLDivElement {

You would have to implement all of the properties and methods of the interface. You probably don't want to do this.

Option 2: Delegation.

You can expose the specific properties and methods you want to make available on your QuizElement class and then delegate to an actual HTMLDivElement instance. Quick example below:

class QuizElement {
    private element: HTMLDivElement;

    constructor(id: string) {
        this.element = <HTMLDivElement>document.getElementById(id);
    }

    set innerHTML(content: string) {
        this.element.innerHTML = content;
    }
}

var quizElement = new QuizElement('quiz');

quizElement.innerHTML = 'Example';

Upvotes: 17

Related Questions