Can you extend a HTMLDivElement in TypeScript?

前端 未结 4 1288
名媛妹妹
名媛妹妹 2021-02-20 12:32

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 p

4条回答
  •  终归单人心
    2021-02-20 13:14

    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 = document.getElementById("myDiv"); 
    
     div.mydata = "test";
    

提交回复
热议问题