Sub topic 9
Declaring Covariance with `out`
Prerequisites
This chapter assumes that you know TypeScript generics, interfaces, function types, structural typing, and the strict compiler option. TypeScript 4.7 or newer is required for variance annotations.
A generic type is covariant in a type parameter when it preserves the direction of a subtype relationship. If Dog is assignable to Animal, then Producer<Dog> is assignable to Producer<Animal> when Producer is covariant.
The key idea is that a covariant type only produces values of its type parameter. A producer of dogs can safely be used where a producer of animals is expected. Every dog is an animal.
Declaring a covariant type parameter
Use the out modifier to declare that a type parameter is covariant:
class Animal {
name = "animal";
}
class Dog extends Animal {
bark() {
return "woof";
}
}
interface Producer<out T> {
get(): T;
}
const dogProducer: Producer<Dog> = {
get: () => new Dog()
};
const animalProducer: Producer<Animal> = dogProducer;
console.log(animalProducer.get().name);
// @ts-expect-error: the assignment direction cannot be reversed
const anotherDogProducer: Producer<Dog> = animalProducer;The out modifier documents the intended relationship and asks the compiler to check that T is used only in output positions. The return type of get is an output position, so this interface satisfies the annotation.
The reverse assignment is unsafe. A Producer<Animal> might return a plain Animal, which does not have the bark method required by Dog.
A useful rule is:
Producer<Dog>can be used asProducer<Animal>.Producer<Animal>cannot be used asProducer<Dog>.
A type parameter can appear in several output positions. Common examples include function return types, readable properties, and values yielded by an iterator.
What `out` prevents
The annotation must match the structure of the type. This declaration is rejected because T is used as an input to write:
interface Writer<out T> {
write: (value: T) => void;
}A function parameter is an input position. If Writer<Dog> were treated as a Writer<Animal>, code could pass a plain Animal to write, even though the writer expects a Dog.
When the annotation helps
TypeScript often infers variance from how a type parameter is used, so simple producers may work without an explicit annotation. Writing out still has practical benefits:
- It communicates the intended API relationship to readers.
- It makes an accidental input use a compiler error.
- It can help the compiler compare complex or recursive generic types more efficiently.
- It records that changing the type's structure may require revisiting its variance.
The annotation does not convert an unsafe design into a safe one. It must describe the actual positions in which the type parameter appears.
Exercise
If Dog is a subtype of Animal, which assignment is valid for interface Producer<out T> { get(): T }?
Show answer
Producer<Dog> is assignable to Producer<Animal>, because the producer only returns values and covariance preserves the subtype direction.