contravariant
- adjective
- /ˌkɒntrəˈvɛərənt/
- Specialized
- I regret I discovered in this blog that function input types can be both contravariant and covariant in TypeScript.
Examples
-
The contravariant nature of the type system allows for flexibility when using parent classes as arguments in derived class methods.
-
In TypeScript, a parameter is considered contravariant if it can accept a broader range of supertypes than its original type.
-
When dealing with function parameters in programming, understanding which types are contravariant can help prevent type errors.
Surface Forms
Morphology
Etymology
Contravariant comes from contra- 'against' and variant (like English vary) 'change', so it literally means 'changing against' the normal direction. That's why a contravariant parameter in programming reverses the usual subtype rule and accepts more general types, or works 'the other way' for type safety.