📚 node [[20210609195030 subtyping]]

Subtyping, at least in programming, is when a typeis somehow related to another type via substitution. A typical example is that Dog is a subtype of Animal. In some cases, the latter could be substituted for the former, whereas the former could always substitute the latter.

The fancy symbol for subtyping is "≼", so "A ≼ B" means that A is a subtype of B.

Subtyping is transitive, meaning that if A ≼ B ≼ C, then A ≼ C.

Assuming A ≼ B,

  • covariance :: when (T -> A) ≼ (T -> B)

  • contravariance :: when (B -> T) ≼ (A -> T)

  • bivariant :: both covariant and contravariant

In TypeScriptand other strongly typed languages, functions are bivariant.

Generally, you want functions to be covariant in their return type and contravariant in their argument type.

References

  1. What are covariance and contravariance? | Stephan Boyer

📖 stoas
⥱ context