Nikita Shchypyplov
Nikita Shchypyplov

Reputation: 1160

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: error image

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!

Upvotes: 0

Views: 83

Answers (1)

mbojko
mbojko

Reputation: 14689

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 { //...

Upvotes: 2

Related Questions