|
|
|
|
|
by Dylan16807
3156 days ago
|
|
Covariant: A variable needs to have an Animal. You can put a Cat in the variable. Contravariant: A variable needs to have a function that accepts Animals. You can't put a function that accepts only Cats, or it will crash on other kinds of animal. But you can put a function that accepts all LivingThings. So when something is covariant you can use a more specific type, and when it's contravariant you can use a more generic type. When it comes to function parameters, typescript lets you use either. You can replace any type with any related type, even if you're going in the wrong direction. They did this to make certain use cases simpler, at the cost of weaker typing. |
|