typescript - Weird error when overloading methods with generics -


i have class implements interface. both define method takes in generic , outputs array of same type generic. i'm getting weird error inconsistencies between class , interface , i'm not sure how solve it. here sample code:

class random implements irandom {     generatearray<t>(method: string): array<t>;     generatearray<t>(constructor: new () => t): array<t>;     generatearray(method: any) {         return new array<any>();     } }  interface irandom {     generatearray<t>(method: string): array<t>;     generatearray<t>(constructor: new () => t): array<t>; } 

here error snippet above code:

class random declares interface irandom not implement it:     types of property 'generatearray' of types 'random' , 'irandom' incompatible:         call signatures of types '{ <t>(method: string): t[]; <t>(constructor: new() => t): t[]; }' , '{ <t>(method: string): t[]; <t>(constructor: new() => t): t[]; }' incompatible:             types of property 'concat' of types '{}[]' , 't[]' incompatible:                 call signatures of types '{ <u extends t[]>(...items: u[]): {}[]; (...items: {}[]): {}[]; }' , '{ <u extends t[]>(...items: u[]): t[]; (...items: t[]): t[]; }' incompatible:                     types of property 'pop' of types '{}[]' , 't[]' incompatible:                         call signatures of types '() => {}' , '() => t' incompatible. 

has encountered error? there way fix it?

you have stumbled across bug - has been fixed (so come out next release)...

in meantime, there 2 workarounds available...

make interface , class generic (rather method).

interface irandom<t> {     generatearray(method: string): t[];     generatearray(constructor: new () => t): t[]; }  class random<t> implements irandom<t> {     generatearray(method: string): t[];     generatearray(constructor: new () => t): t[];     generatearray(method: any) {         return new array<any>();     } } 

or define interface using class (as temporary measure). allow turn tables when bug fixed removing extends random, adding in interface body , putting implements irandom.

interface irandom extends random {  }  class random {     generatearray<t>(method: string): array<t>;     generatearray<t>(constructor: new () => t): array<t>;     generatearray(method: any) {         return new array<any>();     } } 

all of calling code unaware of deception:

function paramisinterface(param: irandom) {     // stuff }  var r = new random();  paramisinterface(r); // accepts random irandom... 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -