Sub topic 5
Function Parameters and `strictFunctionTypes`
Prerequisites
This chapter assumes that you understand TypeScript interfaces, function types, generics, structural typing, and the strict compiler option. You should also know that Dog is a subtype of Animal: every Dog can be used where an Animal is expected.
Variance becomes especially important when a type parameter appears in a function parameter. Function parameters consume values, so their variance direction is opposite to the direction used by function return values.
The Safe Assignment Direction
Consider these types:
class Animal {
name = "animal";
}
class Dog extends Animal {
bark() {
return "woof";
}
}
type AnimalHandler = (animal: Animal) => void;
type DogHandler = (dog: Dog) => void;An AnimalHandler can safely handle a Dog, because a dog is an animal. The reverse is not safe. A DogHandler expects every value to have bark(), but an ordinary Animal might not.
const handlesAnimals: AnimalHandler = (animal) => {
console.log(animal.name);
};
const handlesDogs: DogHandler = (dog) => {
console.log(dog.bark());
};
const dogHandler: DogHandler = handlesAnimals; // OK
// const animalHandler: AnimalHandler = handlesDogs;
// Error under strictFunctionTypesThe assignment to dogHandler is safe. Code that calls it promises to provide a Dog, and handlesAnimals can accept any Animal, including a dog.
The opposite assignment could call handlesDogs with a non-Dog animal. That would violate the handler's expectation. Therefore, function parameter types are checked contravariantly.
What `strictFunctionTypes` Changes
With strict: true, TypeScript enables strictFunctionTypes. It checks function-typed properties contravariantly.
interface AnimalProcessor {
process: (animal: Animal) => void;
}
interface DogProcessor {
process: (dog: Dog) => void;
}
const generalProcessor: AnimalProcessor = {
process: (animal) => console.log(animal.name)
};
const dogProcessor: DogProcessor = generalProcessor; // OK
// const unsafe: AnimalProcessor = {
// process: (dog: Dog) => console.log(dog.bark())
// };
// Error: the parameter is too specificThis rule prevents a function that requires a narrower type from being placed where a function accepting a wider type is required.
| Function position | Variance behavior |
|---|---|
| Return value | Covariant |
| Function-typed property parameter | Contravariant |
| Method parameter declaration | Bivariant in TypeScript |
Why Methods Are Different
TypeScript preserves a compatibility rule for methods. Method parameters are usually checked bivariantly, which means TypeScript permits both assignment directions.
interface AnimalMethod {
process(animal: Animal): void;
}
const dogMethod: AnimalMethod = {
process(dog: Dog) {
console.log(dog.bark());
}
};
dogMethod.process(new Animal()); // Compiles, but can fail at runtimeThe method declaration is accepted even though its implementation requires a Dog. Calling it with an ordinary Animal can make dog.bark() fail. This behavior exists mainly to keep common existing code compatible, especially class and interface hierarchies.
Practical Rule
Use contravariance to ask whether a function can accept every value that the surrounding type might provide. A function that accepts Animal is a suitable replacement for one that accepts Dog. A function that accepts only Dog is not a suitable replacement for one that might receive any Animal.
Exercise
Under strictFunctionTypes, can a value of type (animal: Animal) => void be assigned to (dog: Dog) => void? What about the reverse?
Show answer
The first assignment is allowed because a function accepting any Animal can accept a Dog. The reverse is rejected because a Dog-only function cannot safely handle every Animal.