Skip to main content
contravariant

contravariant

1 9.9
Accepts more general types for a parameter instead of more specific ones
  • adjective
  • /ˌkɒntrəˈvɛərənt/
  • Specialized
: contravariante
  • 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

contravariant positive

Morphology

contravariant = variant (semi-transparent) = contra + variant

The structure 'contra-' + 'variant' transparently signals opposition, but the programmer-facing meaning about accepting supertypes and reversing subtype relationships is domain-specific and not transparently derivable by general learners.

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.