【问题标题】:Extend Element prototype using TypeScript使用 TypeScript 扩展元素原型
【发布时间】:2016-10-23 19:11:06
【问题描述】:

鉴于下面的 javascript,是否有相应的 TypeScript 来做同样的事情?

我只需要扩展 html dom 元素原型,但无法找出正确的 TypeScript 来执行此操作而不会出现编译错误。

Element.prototype.astyle = function actualStyle(props) {
    var el = this;
    var compStyle = window.getComputedStyle(el, null);
    for (var i = 0; i < props.length; i++) {
        var style = compStyle[props[i]];
        if (style != null) {
            return style;
        }
    }
    return null;
};

【问题讨论】:

    标签: javascript html node.js typescript prototype


    【解决方案1】:

    你需要先使用接口来扩展元素。

    interface Element{
        astyle(props:string[]);
    }
    

    【讨论】:

    • 确保将其放入与tsconfig.json 位于同一文件夹的.d.ts 文件中
    猜你喜欢
    • 2019-02-09
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多