Michael Yuan Michael Yuan - 5 months ago 30
HTML Question

Extend Element prototype using TypeScript

Given the javascript like below, is there a corresponding TypeScript to do the same thing?

I just need to extend the html dom element prototype but cannot figure out a correct TypeScript to do this without compile errors.

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;
};

Answer

You need to use interface to extend the Element first.

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