Skip to content

Overload conflicts make implementation impossible #4786

Closed
@omidkrad

Description

@omidkrad

I created type definitions for Aurelia Event Aggregator as below. It compiles fine.

interface Constructor<T> {
    new (...args: any[]): T;
}

declare class EventAggregator
{
    subscribe(event: string, callback: (message: any) => void): void;
    subscribe<T>(event: Constructor<T>, callback: (message: T) => void): void;

    publish(event: string, data?: any): void;
    publish<T>(event: T): void;
}

class SomeMessage {
    constructor(public data: string) {}
}

var ea = new EventAggregator();

// event is string, message is any
ea.subscribe("myEvent", message => {
    console.log(message.anything);
});

// event is class constructor, message is instance of class
ea.subscribe(SomeMessage, message => {
    console.log(message.data);
});

// event is string, message is any 
ea.publish("myEvent", { anything: 1 });

// event is class instance
ea.publish(new SomeMessage(""));

Even though it is possible to define declarations for this class, it is impossible to implement it in TypeScript. The following produces errors, and I can't figure how to fix it.

interface Constructor<T> {
    new (...args: any[]): T;
}

class EventAggregator
{
    subscribe(event: string, callback: (message: any) => void): void;
    subscribe<T>(event: Constructor<T>, callback: (message: T) => void): void {
    }

    publish(event: string, data?: any): void;
    publish<T>(event: T): void {
    }
}

class SomeMessage {
    constructor(public data: string) {}
}

var ea = new EventAggregator();

// event is string, message is any
ea.subscribe("myEvent", message => {
    console.log(message.anything);
});

// event is class constructor, message is instance of class
ea.subscribe(SomeMessage, message => {
    console.log(message.data);
});

// event is string, message is any 
ea.publish("myEvent", { anything: 1 });

// event is class instance
ea.publish(new SomeMessage(""));

Is this a bug?

Here's the original JS source (ES6) for reference: aurelia/event-aggregator

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions