Reputation: 2844
I have legacy code to transform that makes use of mootools. Mootools introduces a new constructor function of 'Element' with two parameters. I did not find any type definition file for mootools, so I have to write my own.
The typescript standard library (lib.d.ts) defines Element as following:
var Element: { prototype: Element; new(): Element; };
which makes it impossible to extend an existing interface.
What I was obviously trying is
interface Element {
new(s: string, s2: any): Element;
}
But still, the compiler complains about
var c = new Element('p',{});
as the contructor expected 0 arguments, but got 2
. What is the preferred workaround here?
The mootools constructor function
var Element = this.Element = function(tag, props){
var konstructor = Element.Constructors[tag];
if (konstructor) return konstructor(props);
if (typeof tag != 'string') return document.id(tag).set(props);
if (!props) props = {};
if (!(/^[\w-]+$/).test(tag)){
var parsed = Slick.parse(tag).expressions[0][0];
tag = (parsed.tag == '*') ? 'div' : parsed.tag;
if (parsed.id && props.id == null) props.id = parsed.id;
var attributes = parsed.attributes;
if (attributes) for (var attr, i = 0, l = attributes.length; i < l; i++){
attr = attributes[i];
if (props[attr.key] != null) continue;
if (attr.value != null && attr.operator == '=') props[attr.key] = attr.value;
else if (!attr.value && !attr.operator) props[attr.key] = true;
}
if (parsed.classList && props['class'] == null) props['class'] = parsed.classList.join(' ');
}
return document.newElement(tag, props);
};
// Browser
var Browser = this.Browser = parse(navigator.userAgent, navigator.platform);
if (Browser.Element){
Element.prototype = Browser.Element.prototype;
// IE8 and IE9 require the wrapping.
Element.prototype._fireEvent = (function(fireEvent){
return function(type, event){
return fireEvent.call(this, type, event);
};
})(Element.prototype.fireEvent);
}
Upvotes: 3
Views: 905
Reputation: 3619
You don't need to extend Element
interface, what you need is to extend type of Element
variable. That's a bit confusing, that's two separate entities from TS standpoint. Here's how you can do it:
declare const Element: { // If you do that in a .d.ts file, you don't need declare.
prototype: Element; // Element here is interface, we leave it unchanged.
new(): Element; // Standard constructor.
new(s: string, n: number): Element; // Your custom constructor.
};
const e = new Element('1', 2);
e.nodeName; // => string, i.e. result object implements Element interface.
Upvotes: 1