In order to address the --strictFunctionTypes issue that impacts how a function's type is assigned to its parameters, it was mentioned earlier.
I felt it was important to provide further illustration of this point.
Illustrative Example
Interactive Playground
https://i.sstatic.net/2DFBB.png
Clarification
'foo' | 'bar'
is considered a subtype of string
, or in TypeScript terms, you could express it as 'foo' | 'bar' extends string
.
When utilized as parameters for function F
, the assignability behaves differently.
The equation
F<string> extends F<'foo' | 'bar'>
showcases contravariance within the type parameter.
In the example above, there are two tests conducted for the function independently and then with the bivariance workaround implemented. The first test explores contravariance, while the second examines covariance.
In absence of the workaround, the function displays contravariant behavior exclusively. However, after applying the hack, it exhibits both contravariant and covariant characteristics, making it bivariant.
An informative article on this specific concept in TypeScript can be found here.