Sub topic 3
strictFunctionTypes and Method Bivariance
Prerequisites: you should know interfaces, function types, arrays, and basic compiler errors.
With strictFunctionTypes, function parameters are checked contravariantly. A function that accepts any Animal can replace one that receives a Dog, but a function that requires a Dog cannot replace one that might receive another animal.
Method syntax is an exception. Methods remain bivariant, so TypeScript accepts some unsafe parameter assignments for compatibility with existing JavaScript patterns.
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 A handler requiring Dog cannot handle every Animal.
const unsafe: AnimalHandler = dogHandler;
interface MethodHandler {
handle(animal: Animal): void;
}
const methods: MethodHandler = {
handle(dog: Dog) {
dog.bark();
}
};Arrays, Tuples, and Generic Types
Arrays are covariant: Dog[] can be assigned to Animal[]. This is convenient but unsound because the target can then insert a non-Dog. Prefer readonly Animal[] when a function only reads values. Tuples follow similar rules, including fixed positions and lengths.
Generic compatibility depends on how a type parameter is used:
| Position | Relationship | Example |
|---|---|---|
| Output only | Covariant | `Producer<Dog>` fits `Producer<Animal>` |
| Input only | Contravariant | `Consumer<Animal>` fits `Consumer<Dog>` |
| Input and output | Invariant | `Box<Dog>` does not fit `Box<Animal>` |
TypeScript can document these relationships with in and out. The annotations must agree with the actual structure.
interface Producer<out T> {
get(): T;
}
interface Consumer<in T> {
set(value: T): void;
}
interface Box<in out T> {
get(): T;
set(value: T): void;
}Nominal Exceptions and Intentional Unsoundness
Classes with private or protected members are compatible only when those members come from the same declaration. Separate enums are also incompatible, even when they have identical members. Numeric enum values can still be assigned to number.
Method bivariance and array covariance are intentionally unsound trade-offs. They preserve useful JavaScript patterns, so use readonly collections and function properties when safety matters.
A brand adds nominal-style checking to a structural type:
type UserId = string & { readonly __brand: "UserId" };
const userId = "u1" as UserId;Type assertions do not convert runtime values. An assertion must usually involve comparable types; value as unknown as Target can bypass that check, so use it only when a real invariant exists.
Exercise
Why is readonly Animal[] safer than Animal[] when a collection comes from Dog[]?
Show answer
readonly Animal[] prevents writes, so a caller cannot insert a non-Dog into the original Dog[] collection.