简体   繁体   中英

Why TypeScript complains about parent class inheritance?

So, I have this code base:

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
}

Using this code I am keep getting this error: 错误图片

I was trying to add .d.ts file as this:

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
}

But it does not fix an error.

What causes this error?

Thanks in advance!

Mixing conventions (prototype and class) seems like asking for trouble (and the source of confusion for TypeScript, likely). Just stick to one pattern:

class MicroEvent {
    bind //...
    unbind  //...
    trigger //...
};

class PluginManager extends MicroEvent { //...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM