Course contentsShow
← TypeScript
Lesson 1197 of 1,79628. tsconfig in DepthFree lesson

strictFunctionTypes: Contravariant Function Parameters

Why strictFunctionTypes enforces sound parameter type checking and prevents unsafe function assignments.

strictFunctionTypes: Contravariant Function Parameters

What you'll learn: How strictFunctionTypes prevents unsafe function assignments by enforcing correct parameter type checking.

The Problem It Solves

Without strictFunctionTypes, TypeScript uses a bivariant check for function parameter types—meaning you can assign a function expecting a more specific type to a variable typed with a function expecting a more general type, and vice versa. This bidirectional flexibility is unsound and can cause runtime errors.

With strictFunctionTypes enabled, TypeScript enforces contravariant parameter checking for function types, which is the mathematically correct way to ensure type safety.

What Does Contravariant Mean?

Contravariance means that function parameters vary in the opposite direction of assignability:

  • A function accepting a more general type can substitute for a function accepting a more specific type
  • But NOT the reverse

Think of it like hiring: if you need someone who can "cook Italian food," you can hire someone who can "cook any food" (more general). But you cannot hire someone who only "cooks pizza" (more specific) to fill a "cook any food" role.

How strictFunctionTypes Prevents Bugs

type Animal = { name: string };
type Dog = { name: string; bark: () => void };

let logAnimal: (animal: Animal) => void;
let logDog: (dog: Dog) => void;

// Without strictFunctionTypes: allowed (unsafe!)
// With strictFunctionTypes: ERROR
logAnimal = logDog; // ❌ Type '(dog: Dog) => void' not assignable

// This direction is safe and allowed:
logDog = logAnimal; // ✅ OK

Why? If logAnimal were assigned logDog, calling it with a plain Animal (like a Cat) would crash when the function tries to call .bark().

Key Takeaway: strictFunctionTypes enforces contravariant parameter checking, preventing unsafe function assignments where a function expecting specific types could receive more general values it can't handle.