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
I'm trying to use specialized signatures with string literal types to write typed events. Everything is good in single class or interface. But it doesn't work with inheritance.
Code to reproduce
classObservable{// subscribe to "change" eventbind(eventName: "change",callback: (sender: this,args: ObservableChangeArgs)=>void): this;// subscribe to other eventsbind(eventName: string,callback: Function): this;bind(eventName: string,callback: Function): this {// some implementation herereturnthis;}}interfaceObservableChangeArgs{prop?: string;value?: any;}classViewextendsObservable{}interfaceViewextendsObservable{// subscribe to "render" eventbind(eventName: "render",callback: (sender: this,args: ViewRenderArgs)=>void): this;// subscribe to other eventsbind(eventName: string,callback: Function,context?: any): this;}interfaceViewRenderArgs{domElement: HTMLElement;}letview=newView();view.bind("render",(sender,args)=>{console.log(args.domElement.tagName);// Ok}).bind("render",(sender,args)=>{console.log(args.unknownProperty);// Error, this is expected}).bind("change",(sender,args)=>{console.log(args.prop);// Ok}).bind("change",(sender,args)=>{console.log(args.unknownProperty);// Also Ok, but error is expected});
I expect an error in last callback because change event is defined in the parent class and unknownProperty is not declared there. I also tried overload bind method directly in View class or use just interfaces instead of classes. No luck:( A workaround is to copy all signatures from parent class to child. But this is not good way.
Reproduced in 1.8.10 and also in typescript@next 2.1.0-dev.20160809
The text was updated successfully, but these errors were encountered:
A workaround is to copy all signatures from parent class to child.
Unfortunately this is currently the only way to accomplish what you're trying to do. We've been trying to figure out how to "correctly" copy down signatures from one class to another but there is a lot of ambiguity in what the intent of signature merging is in many cases.
Hey, this can now be kinda-sorta automated with a bunch of derived-type magic. I used the code from this SO answer to make an interface mapping specialized signature literal values to the arguments that get passed to the rest of the function. This means that in your subclass, if you want to add more overloads, you just make an interface for the subclass's new events and inherit from the superclass's event interface.
It's a bunch of extra boilerplate but if you have a lot of subclasses adding specialization, it can save tons of repetition. (Thanks again, @dragomirtitian!)
I'm trying to use specialized signatures with string literal types to write typed events. Everything is good in single class or interface. But it doesn't work with inheritance.
Code to reproduce
I expect an error in last callback because
change
event is defined in the parent class andunknownProperty
is not declared there. I also tried overloadbind
method directly inView
class or use just interfaces instead of classes. No luck:( A workaround is to copy all signatures from parent class to child. But this is not good way.Reproduced in
1.8.10
and also intypescript@next 2.1.0-dev.20160809
The text was updated successfully, but these errors were encountered: