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
{{ message }}
Repository navigation
Covariance, contravariance and assignability #55603
If the methods defined in the Queue type (without using in and out keywords) used the arrow function syntax, there are two of the four assignments which are errors.
But if using the normal function syntax, only one is erroring.
Methods are always bivariant in their argument, while function properties are contravariant in their argument under strictFunctionTypes. More discussion here.
And from the linked issue:
Methods are excluded specifically to ensure generic classes and interfaces (such as Array<T>) continue to mostly relate covariantly. The impact of strictly checking methods would be a much bigger breaking change as a large number of generic types would become invariant (even so, we may continue to explore this stricter mode).
Agreed. But then, in the latter case, the missing error pops back up when the generic is specified to be in out.
Does that mean those keywords override bivariance 🤔 ?
Don't rely too much on these variance annotations, as they may produce unexpected behavior when fall back to structure checking. You can see the discussion here.
Activity
[-]Covariance contravariance and assignability[/-][+]Covariance, contravariance and assignability[/+]fatcerberus commentedon Sep 1, 2023
Methods are intentionally bivariant, this is mentioned in the FAQ:
https://github.com/microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs
And from the linked issue:
aswinsvijay commentedon Sep 1, 2023
Agreed. But then, in the latter case, the missing error pops back up when the generic is specified to be
in out.Does that mean those keywords override bivariance 🤔 ?
whzx5byb commentedon Sep 1, 2023
Aswin S Vijay (@aswinsvijay)
Don't rely too much on these variance annotations, as they may produce unexpected behavior when fall back to structure checking. You can see the discussion here.
andrewbranch commentedon Sep 1, 2023
Yes.
typescript-bot commentedon Sep 4, 2023
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.