Naibaf
Naibaf

Reputation: 111

Typescript declaration merging library types

does somebody know how to "extend" (via declaration merging) the declarations of interfaces, that where declared in a library file from typescript?

In my case i want to extend the interface HTMLCanvasElement of the typescript build in library lib.dom. I know how declaration merging is working but i don't get the right imports for the interface HTMLCanvasElement.

import {HTMLCanvasElement} from '<what.to.use.here>';

declare module '<what.to.use.here>'{
    interface HTMLCanvasElement{
        //add my stuff
    }
}

Thanks :)

Upvotes: 6

Views: 2686

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249466

Those types live in the global namespace. If you are in a script file (ie not a module) you can just redeclare it. If you are in a module, you need to declare it in global no import required.

declare global{
    interface HTMLCanvasElement{
        my:number
    }
}

export var x = 1;// just to make this module
let c: HTMLCanvasElement;
c.animate // regular stuff 
c.my //my stuff 

Upvotes: 5

Related Questions