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
Trying to convert function overloads to union type, I'm getting errors.
Here is a very simple code that shows the error:
classAnimal{run(){}}classCatextendsAnimal{}classDogextendsAnimal{}functionrun(a: Animal){a.run();}functionf<TextendsCat|Dog>(a: T){a.run();// OK.run(a);// Argument of type 'T' is not assignable to parameter of type 'Animal'.}
I don't understand the difference TS makes between a.run() and run(a).
The text was updated successfully, but these errors were encountered:
It's true that the spec predicts that T is assignable to Animal in section 3.10.4. Namely the final bullet,
T is an object type, a type parameter, or the Number, Boolean, or String primitive type, Animal is an object type, and for each member M ("run") in Animal,
M ("run") is a property and T has an apparent property N "run"
and so forth.
It is possible to fix the compiler to match this behavior. However, it does not scale to union types. For example, even if the compiler matches the spec, it would not work if the run function took a Cat | Dog instead of an Animal.
I believe the fix we need here (and it needs to be fixed in the spec as well) is that if the source type is a type parameter whose base constraint is a union type, then we need to check if that union type is assignable to the target. I will try to make this change, and see what the outcome is. Then we can change it in the spec if everybody is happy.
@ahejlsberg what do you think of this suggestion, barring any unexpected outcome?
Trying to convert function overloads to union type, I'm getting errors.
Here is a very simple code that shows the error:
I don't understand the difference TS makes between
a.run()
andrun(a)
.The text was updated successfully, but these errors were encountered: