Sub topic 6
Why Method Declarations Stay Bivariant
This chapter assumes that you know TypeScript generics, interfaces, structural typing, and the strict compiler option. It also assumes that you can distinguish a function type from a method declaration.
Under strictFunctionTypes, TypeScript checks function-typed properties more strictly than method declarations. Function parameters are checked contravariantly, but method parameters remain bivariant.
The difference between a property and a method
Consider two interfaces with the same apparent operation:
class Animal {
name = "animal";
}
class Dog extends Animal {
bark() {
return "woof";
}
}
interface AnimalPropertyHandler {
handle: (animal: Animal) => void;
}
interface AnimalMethodHandler {
handle(animal: Animal): void;
}
const dogPropertyHandler: { handle: (dog: Dog) => void } = {
handle: dog => console.log(dog.bark())
};
const dogMethodHandler: { handle(dog: Dog): void } = {
handle(dog) {
console.log(dog.bark());
}
};
// Compile with "strict": true.
// const propertyHandler: AnimalPropertyHandler = dogPropertyHandler;
// Error: the property cannot safely handle every Animal.
const methodHandler: AnimalMethodHandler = dogMethodHandler;
// Allowed: method parameters remain bivariant.The property assignment is rejected because dogPropertyHandler can handle only Dog values. An AnimalPropertyHandler must also handle an Animal that is not a dog.
The method assignment is accepted for historical compatibility. TypeScript allows the narrower Dog parameter where the interface declares Animal. This is convenient, but it is not fully sound:
methodHandler.handle(new Animal());
// This compiles, but dogMethodHandler.handle receives a non-Dog Animal.
// Calling dog.bark() would fail at runtime.A type system is sound when accepted programs cannot break because of a type mismatch. TypeScript deliberately allows some unsound patterns to remain compatible with existing JavaScript and declaration files.
Why methods use bivariant checking
TypeScript introduced stricter checking for function parameters through strictFunctionTypes, but method declarations were excluded. Many existing APIs, especially browser APIs in the DOM libraries, use method syntax with parameter relationships that would fail under fully contravariant checking.
Changing those declarations could have caused widespread errors in valid programs. Keeping method parameters bivariant preserved compatibility, even though it permits unsafe assignments.
The assignment direction can be summarized like this:
Prefer function properties for safer callbacks
Use a function-typed property when the parameter type must be checked contravariantly:
interface AnimalConsumer {
accept: (animal: Animal) => void;
}
interface DogConsumer {
accept: (dog: Dog) => void;
}
const animalConsumer: AnimalConsumer = {
accept: animal => console.log(animal.name)
};
const dogConsumer: DogConsumer = animalConsumer;
// Safe: a function that accepts every Animal can accept a Dog.The reverse assignment is unsafe and is rejected:
const onlyDogs: DogConsumer = {
accept: dog => console.log(dog.bark())
};
// const unsafe: AnimalConsumer = onlyDogs;
// Error under strictFunctionTypes.The distinction matters most for callbacks, event handlers, and public interfaces. Method syntax communicates an object operation, but it also opts into the older bivariant parameter behavior.
Exercise
Exercise
Why is an object with a method accepting only Dog assignable to an interface whose method accepts Animal, while the equivalent function property is rejected?
Show answer
Method parameters remain bivariant for compatibility with existing TypeScript and DOM APIs. Function-typed properties follow strict contravariant checking under strictFunctionTypes, so a function that cannot handle every Animal is rejected.