Contravariance
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."
Related Terms
Covariance
Covariance means 'if a Dog is an Animal, then a Box of Dogs is also a Box of Animals.
Functional Programming
Functional programming is like cooking with strict rules: no shared bowls, no side dishes contaminating each other, and every dish must be exactly reproduc...
Structural Typing
Structural typing is TypeScript's dating philosophy: it doesn't care about your name, only your shape.
TypeScript
TypeScript is JavaScript with a strict parent watching over it.