因此,我有以下代码库:
var MicroEvent = function(){}
MicroEvent.prototype = {
bind : function(event, fct){
this._events = this._events || {};
this._events[event] = this._events[event] || [];
this._events[event].push(fct);
},
unbind : function(event, fct){
this._events = this._events || {};
if( event in this._events === false ) return;
this._events[event].splice(this._events[event].indexOf(fct), 1);
},
trigger : function(event /* , args... */){
this._events = this._events || {};
if( event in this._events === false ) return;
for(var i = 0; i < this._events[event].length; i++){
this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1))
}
}
};
class PluginManager extends MicroEvent {
registerPlugin(pluginName: string, applicationName: string, active: boolean = true) {
// code
}
// ...code
}
我试图以此添加.d.ts文件:
declare interface MicroEvent {
registerPlugin(arg1: string, arg2: string, arg3?: boolean): void;
bind(event: string, fct: string):void
unbind(event: string, fct: string):void
trigger(event: string):void
}
但是它不能解决错误。
什么原因导致此错误?
谢谢!
答案 0 :(得分:2)
混合约定(原型和类)看起来很麻烦(可能还会引起TypeScript的混乱)。只需坚持一种模式:
class MicroEvent {
bind //...
unbind //...
trigger //...
};
class PluginManager extends MicroEvent { //...