Sub topic 10
Parameter Variance and `strictFunctionTypes`
Assume you know TypeScript function types, interfaces, and subtype relationships. This chapter focuses on how TypeScript decides whether one function can replace another, especially when their parameter types differ.
The replacement rule
Suppose a program expects a function that accepts an Animal:
type AnimalHandler = (animal: Animal) => void;The replacement function must be safe for every Animal that the program might pass. A function that accepts any Animal is safe. A function that accepts only a Dog is not safe because the program could pass a Cat.
This rule makes parameter types contravariant. A function with a wider parameter type can replace a function with a narrower parameter type.
interface Animal {
name: string;
}
interface Dog extends Animal {
bark(): void;
}
type DogHandler = (dog: Dog) => void;
const handlesAnyAnimal: (animal: Animal) => void = animal => {
console.log(animal.name);
};
const dogHandler: DogHandler = handlesAnyAnimal; // OK
const onlyHandlesDogs: DogHandler = dog => {
dog.bark();
};
// Error with strictFunctionTypes:
// a function expecting Dog cannot handle every Animal
const animalHandler: (animal: Animal) => void = onlyHandlesDogs;The names “source” and “target” help when reading an assignment:
- The target is the type on the left. It describes how the function will be used.
- The source is the value on the right. It is the function being assigned.
- The source must safely support every call allowed by the target.
Return types use the opposite direction
Return values are covariant. If a caller expects an Animal, a function returning a more specific Dog is safe because every Dog is an Animal.
type AnimalFactory = () => Animal;
type DogFactory = () => Dog;
const makeDog: DogFactory = () => ({
name: "Rex",
bark() {
console.log("woof");
}
});
const makeAnimal: AnimalFactory = makeDog; // OKThe reverse assignment is unsafe. A function that returns any Animal might return a Cat, so it cannot replace a function that promises to return a Dog.
A useful table summarizes the directions:
| Function position | Safe replacement usually needs |
|---|---|
| Parameter | A wider or equal input type |
| Return value | A narrower or equal output type |
| Number of parameters | The replacement may use fewer parameters |
Why `strictFunctionTypes` matters
With strict: true, TypeScript enables strictFunctionTypes. It checks function parameters contravariantly instead of allowing an unsafe assignment in both directions.
Without this check, callback code could fail at runtime:
interface AppEvent {
kind: string;
}
interface ClickEvent extends AppEvent {
x: number;
y: number;
}
function runHandler(
handler: (event: AppEvent) => void,
event: AppEvent
): void {
handler(event);
}
const clickOnly = (event: ClickEvent) => {
console.log(event.x, event.y);
};
// Error under strictFunctionTypes.
// runHandler could pass an AppEvent without x and y.
runHandler(clickOnly, { kind: "connected" });A common error is to read the assignment from the handler’s point of view. Instead, inspect how the target will call it. Ask: “Could the target pass an input that this function cannot handle?”
Parameter count and method syntax
A function that ignores extra arguments is safe:
type Logger = (message: string) => void;
const logger: Logger = () => {
console.log("called");
};The reverse is not safe if the target supplies only one argument but the function requires two.
One historical exception is method syntax. Under strictFunctionTypes, parameters in methods and constructors can still use more permissive bivariance. Prefer function-property syntax when you want strict callback checking:
interface SafeHandlers {
onEvent: (event: AppEvent) => void;
}Exercise
Is (animal: Animal) => void assignable to (dog: Dog) => void, and why?
Show answer
Yes. The function accepts every Dog because every Dog is an Animal. The reverse assignment is unsafe because a function accepting only Dog cannot handle every Animal.