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
When deriving from a class which methods are too generic it's usually desirable to restrict method signatures in the subclass without changing the implementation. It would be great to have an ability to narrow declarations.
Code
import{EventEmitter}from'events'classMyClassextendsEventEmitter{on(event: 'foo',fn: (d: number)=>void): this
on(event: 'bar',fn: (d: string)=>void): this
}constinstance=newMyClass()instance.on('foo',(n)=>{console.log(n+42)})// Okinstance.on('xyz',(d)=>{console.log(d)})// Error
Expected behavior:
Compiles correctly if there are no type errors like on the last line.
Actual behavior:
Function implementation is missing or not immediately following the declaration.
(method) MyClass.on(event: "bar", fn: (d: string) => void): this (+1 overload)
Related:
I use the following workaround, because it has the least boilerplate, but it is not ideal since it has a runtime dependency (i.e. additional function call):
import{EventEmitter}from'events'classMyClassextendsEventEmitter{on(event: 'foo',fn: (d: number)=>void): this
on(event: 'bar',fn: (d: string)=>void): this
on(event: any,fn: (...args: any[])=>void){returnsuper.on(event,fn)}}constinstance=newMyClass()instance.on('foo',(n)=>{console.log(n+42)})// OKinstance.on('xyz',(d)=>{console.log(d)})// Error
Also, is this issue somehow related to the problem I've described?
The text was updated successfully, but these errors were encountered:
baygeldin
changed the title
Allow overloading/overriding method signatues in subclasses (i.e. narrowing declarations)
Allow overloading/overriding method signatures in subclasses (i.e. narrowing declarations)
Jan 15, 2018
One thing to note, that it does not matter whether you put it in a class or in an interface, the new definitions of on override those of the base. in other words, MyClass.on only has two overloads here one that accepts foo and another for bar, but the base signature that accepts string is not there any longer as it was overwritten.
@mhegazy interesting, so it works because class generates interface declarations (as pointed out in docs) and because interface declarations of the same module are merged. I guess I need to revisit what interfaces in TypeScript really are... Thank you, it works.
TypeScript Version: 2.6.2
When deriving from a class which methods are too generic it's usually desirable to restrict method signatures in the subclass without changing the implementation. It would be great to have an ability to narrow declarations.
Code
Expected behavior:
Compiles correctly if there are no type errors like on the last line.
Actual behavior:
Function implementation is missing or not immediately following the declaration.
(method) MyClass.on(event: "bar", fn: (d: string) => void): this (+1 overload)
Related:
I use the following workaround, because it has the least boilerplate, but it is not ideal since it has a runtime dependency (i.e. additional function call):
Also, is this issue somehow related to the problem I've described?
The text was updated successfully, but these errors were encountered: