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
Hi
I stumbled on a strange behaviour which in my understanding is a bug (in Node with Typescript 1.6 and reproduced in the online Playground)
B is a subclass of A
It is possible to assign a variable F of type "function with A argument" with a value "function with a B argument".
This means that when calling F with a value of type A, the concrete function called will use some specific properties of B which are missing, leading to a runtime error
Olivier
classMammal{}classWhaleextendsMammal{swim(duration:number){/* some code */}}functionswim10(w:Whale){w.swim(10);}typemammalFunc=(m:Mammal)=>void;functioneach(m:Mammal[],f:(Mammal)=>void){m.forEach(f);}varf1:mammalFunc=swim10;// Problem : this should not be possiblevarm1=newMammal();f1(m1);// this compiles, but will fail at runtime because m1 has no swim() methodeach([m1],swim10);// this compiles, but will also fail at runtime
The text was updated successfully, but these errors were encountered:
This is by design. See #222 and the explanation here. It is certainly possible to design rules that would disallow the example above (e.g. we could have strict contra-variance for parameters), but it would have far ranging undesired effects. For example, with strict parameter contra-variance, arrays would become invariant and a Whale[] wouldn't be assignable to a Mammal[]. That runs counter to everyone's expectations. So, in short, by fixing this we'd just trade one problem for another.
Hi
I stumbled on a strange behaviour which in my understanding is a bug (in Node with Typescript 1.6 and reproduced in the online Playground)
B is a subclass of A
It is possible to assign a variable F of type "function with A argument" with a value "function with a B argument".
This means that when calling F with a value of type A, the concrete function called will use some specific properties of B which are missing, leading to a runtime error
Olivier
The text was updated successfully, but these errors were encountered: