You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Overload conflicts make implementation impossible #4786
interfaceConstructor<T>{new(...args: any[]): T;}declareclassEventAggregator{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;}classSomeMessage{constructor(publicdata: string){}}varea=newEventAggregator();// event is string, message is anyea.subscribe("myEvent",message=>{console.log(message.anything);});// event is class constructor, message is instance of classea.subscribe(SomeMessage,message=>{console.log(message.data);});// event is string, message is any ea.publish("myEvent",{anything: 1});// event is class instanceea.publish(newSomeMessage(""));
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.
interfaceConstructor<T>{new(...args: any[]): T;}classEventAggregator{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{}}classSomeMessage{constructor(publicdata: string){}}varea=newEventAggregator();// event is string, message is anyea.subscribe("myEvent",message=>{console.log(message.anything);});// event is class constructor, message is instance of classea.subscribe(SomeMessage,message=>{console.log(message.data);});// event is string, message is any ea.publish("myEvent",{anything: 1});// event is class instanceea.publish(newSomeMessage(""));
method definition is not counted as part of the overload set. you need to 1. define an overload signature of each overload, and 2. have an implementation signature that is compatible with all the overloads.
This is a common source of confusion. Can we improve the error here? If the assignability check fails could we then check assignability against the implementation signature to include more helpful error text (or have we already thrown away that context by then)? That might help people solve the issue without having to go from the IDE to StackOverflow/GitHub/etc.
Is there any way to say that a parameter can take only object instances and not class constructors, so that for example ea.publish(SomeMessage) is not allowed?
Activity
[-]Overload conflicts[/-][+]Overload conflicts make implementation impossible[/+]mhegazy commentedon Sep 14, 2015
method definition is not counted as part of the overload set. you need to 1. define an overload signature of each overload, and 2. have an implementation signature that is compatible with all the overloads.
danquirk commentedon Sep 14, 2015
This is a common source of confusion. Can we improve the error here? If the assignability check fails could we then check assignability against the implementation signature to include more helpful error text (or have we already thrown away that context by then)? That might help people solve the issue without having to go from the IDE to StackOverflow/GitHub/etc.
omidkrad commentedon Sep 14, 2015
Mohamed Hegazy (@mhegazy) that worked. Thanks.
Is there any way to say that a parameter can take only object instances and not class constructors, so that for example
ea.publish(SomeMessage)is not allowed?danquirk commentedon Sep 14, 2015
A hacky option:
omidkrad commentedon Sep 15, 2015
Thank you very much. I'm closing this question. You may want to open a separate issue for improving the error message for this case.