Skip to content

Contravariance

Spicy — senior dev territoryGeneral Dev

ELI5 — The Vibe Check

Contravariance is covariance's weird sibling that goes the opposite direction. If a Dog is an Animal, a handler that accepts any Animal can substitute for one that accepts Dogs. It's backwards, but it's correct, and it'll hurt your brain the first time.

Real Talk

A type system concept where the subtyping relationship is reversed for a type constructor. Function parameter types are contravariant — a function accepting Animal can safely substitute for one accepting Dog (since it handles all animals, it can handle dogs). TypeScript enforces this in strict mode with strictFunctionTypes.

When You'll Hear This

"Function parameters are contravariant — that's why you can pass (animal: Animal) => void where (dog: Dog) => void is expected." / "Understanding contravariance is the moment you truly level up in type theory."

Made with passive-aggressive love by manoga.digital. Powered by Claude.