Typescript声明合并库类型

6

有人知道如何通过声明合并来“扩展”typescript库文件中声明的接口吗?

在我的例子中,我想要扩展typescript内置库中的接口HTMLCanvasElement。我知道如何使用声明合并,但是我没有得到接口HTMLCanvasElement的正确导入。

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

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

感谢您的信任 :)
2个回答

5

这些类型属于全局命名空间。如果你在脚本文件中(即不是模块)你可以重新声明它。如果你在模块中,你需要在 global 中声明,不需要导入。

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 

-2

声明合并是指编译器将使用相同名称声明的两个单独声明合并为一个定义。

请参见以下示例,其中typescript中的接口声明已合并:

    interface Boy {
    height: number;
    weight: number;
    }

    interface Boy {
    mark: number;
    }

    let boy: Boy = {height: 6 , weight: 50, mark: 50}; 

声明合并


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接