I need to extend the interface element of the house. Implemented in this way. Here everything works without errors.

interface MyTileElement extends HTMLElement { tile: Tile; } interface Document { createElement(tagName: "space-tile"): MyTileElement; } class Tile { element: MyTileElement; constructor(){ this.element = document.createElement("space-tile"); this.element.tile = this; } } let n = 2; 

But it is worth adding export or import, immediately typescript throws an exception:

 interface MyTileElement extends HTMLElement { tile: Tile; } interface Document { createElement(tagName: "space-tile"): MyTileElement; } class Tile { element: MyTileElement; constructor(){ this.element = document.createElement("space-tile"); //Error:(13, 9) TS2322:Type 'HTMLElement' is not assignable to type 'MyTileElement'. // Property 'tile' is missing in type 'HTMLElement'. this.element.tile = this; } } let n = 2; export {n} // Добавленная строчка 

I would like to know why this happens and how to get out of the difficulty.

    1 answer 1

    Why this error occurs, I can not answer, but I can offer a solution: you need to move the interfaces to a separate file. However, it is necessary to allocate the interface from the Tile class in order to terminate the circular dependency with the MyTileElement type.

    First file:

     interface ITile { element: MyTileElement; } interface MyTileElement extends HTMLElement { tile: ITile; } interface Document { createElement(tagName: "space-tile"): MyTileElement; } 

    Second file:

     class Tile implements ITile { element: MyTileElement; constructor(){ this.element = document.createElement("space-tile"); this.element.tile = this; } } let n = 2; export {n} 
    • Did not help. Import interfaces do, and the same error. - Mr Fix
    • @Mr. Fix and if the first file is made as .d.ts , then the interfaces will be visible everywhere without import, or is it not suitable? - kmv
    • maybe something else needs to be done? File renamed, I try differently, but does not see interfaces, without import. - Mr. Fix
    • @MisterFix Could you add a new code to the question that does not work? - kmv
    • Decided through neymspeysy. Made one space, and broke it into files. That's enough for now. I have some. Basically, the reasons for this behavior were interesting, but I look at Russian typing very tightly. - Mr Fix