Sub topic 12
`strictFunctionTypes` and Method Bivariance
Prerequisites and the checking direction
This chapter assumes you know TypeScript functions, interfaces, and assignability. It also assumes you have seen strict: true in a tsconfig.json file.
When TypeScript compares two function types, it checks whether a value of the source type can safely be used where the target type is expected. The parameter rule can feel backward:
- A function that accepts a broad type can be used where a function accepting a narrow type is needed.
- A function that accepts only a narrow type cannot safely be used where a broad type is needed.
interface Animal {
name: string;
}
interface Dog extends Animal {
bark(): void;
}
type AnimalHandler = (animal: Animal) => void;
type DogHandler = (dog: Dog) => void;
const animalHandler: AnimalHandler = animal => {
console.log(animal.name);
};
const dogHandler: DogHandler = dog => {
dog.bark();
};
const safe: DogHandler = animalHandler;
// @ts-expect-error DogHandler cannot handle every Animal
const unsafe: AnimalHandler = dogHandler;A DogHandler might receive a cat when used as an AnimalHandler. The call would then fail because cats do not have bark. Assigning the broader AnimalHandler to DogHandler is safe because that handler uses only properties every animal has.
What `strictFunctionTypes` changes
strictFunctionTypes enables contravariant checking for function parameters. Contravariance means that the source parameter type must be at least as broad as the target parameter type.
The option is included in strict mode:
{
"compilerOptions": {
"strict": true
}
}Return types use the opposite relationship. A function that returns a Dog can be used where a function returning an Animal is expected, because every dog is an animal.
Why method syntax is bivariant
TypeScript keeps a compatibility exception for methods and constructors. Their parameters are bivariant, meaning TypeScript allows both parameter directions. This preserves compatibility with existing JavaScript patterns, especially mutable classes and arrays, but it can permit an unsafe assignment.
interface MethodStyle {
handle(animal: Animal): void;
}
interface PropertyStyle {
handle: (animal: Animal) => void;
}
const dogMethod = {
handle(dog: Dog) {
dog.bark();
}
};
const acceptedMethod: MethodStyle = dogMethod;
// @ts-expect-error Function-valued properties use strict parameter checking
const rejectedProperty: PropertyStyle = dogMethod;Prefer function-valued properties when callback safety matters:
interface SafeComponent {
onAnimal: (animal: Animal) => void;
}The property form receives the stricter strictFunctionTypes check. Method syntax is convenient, but it accepts a narrower parameter than the interface promises.
Exercise
Why is an AnimalHandler assignable to DogHandler, but DogHandler is not assignable to AnimalHandler under strictFunctionTypes?
Show answer
An AnimalHandler can process every Dog because Dog extends Animal. A DogHandler cannot process every Animal because some Animals may not have bark.