2

Compiler Error-

Class 'MessageBus' incorrectly implements interface 'IMessageBus'. Property 'dispatch' is missing in type 'MessageBus'.

IMessageBus Interface-

export interface IMessageBus {
  dispatch: (eventName: string, info?: any) => void;
  listen: (eventName: string, callback: Function) => void;
}

MessageBus Class-

import {IMessageBus} from './IMessageBus';

export class MessageBus implements IMessageBus {
  static listeners: Object[] = [];
  public static dispatch(event: string, info?: any): void {
     this.listeners
        .forEach((l) => {
           if (l["event"] === event) {
               l["cb"](info);
           }
        });
  }

  public static listen(event:string, cb: (any) => any):void {
    this.listeners.push({event: event, cb: cb});
  }
}

Please advice on how to resolve this.

1

2 Answers 2

3

Thierry's answer is correct.

Your interface is not defined wrong. But a more idiomatic definition would look like:

export interface IMessageBus {
  dispatch(eventName: string, info?: any): void;
  listen(eventName: string, callback: Function): void;
} 
Sign up to request clarification or add additional context in comments.

Comments

2

I think that you should remove the static keyword for your methods in the class implementing the interface:

export class MessageBus implements IMessageBus {
  listeners: Object[] = [];
  public dispatch(event: string, info?: any): void {
    this.listeners
      .forEach((l) => {
        if (l["event"] === event) {
           l["cb"](info);
        }
      });
  }

  public listen(event:string, cb: (any) => any):void {
    this.listeners.push({event: event, cb: cb});
  }
}

2 Comments

Hi Thierry, Thanks for the reply. Although I am not looking to instantiate new instance of MessageBus. Hence the need for static. This should do the trick. stackoverflow.com/questions/20992514/…
Hey. I see the problem. Yes, this question is definitely what you're looking for. I didn't know the "assign the class to the interface" feature of TypeScript. But it's a cool one ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.