Sub topic 2
Covariance: Types That Produce Values
Prerequisites and the core idea
This chapter assumes that you understand TypeScript interfaces, generics, structural typing, and basic assignability.
Covariance describes how a subtype relationship between type arguments carries over to a generic type. If Dog is a subtype of Animal, then a covariant producer of Dog can be used where a producer of Animal is expected.
A producer is a type that gives values to its caller. Its type parameter appears in an output position, such as a function's return type.
class Animal {
constructor(public name: string) {}
}
class Dog extends Animal {
bark() {
return "woof";
}
}
interface Producer<out T> {
get(): T;
}
class DogProducer implements Producer<Dog> {
get() {
return new Dog("Rex");
}
}
const dogProducer: Producer<Dog> = new DogProducer();
// Dog is an Animal, so this assignment is safe.
const animalProducer: Producer<Animal> = dogProducer;
console.log(animalProducer.get().name); // RexThe assignment is safe because the consumer of animalProducer can only ask for an Animal. A Dog is an Animal, so the producer always supplies an acceptable value.
The reverse assignment is unsafe:
interface Producer<out T> {
get(): T;
}
declare const animalProducer: Producer<Animal>;
// Error: an Animal producer might return a non-Dog.
// const dogProducer: Producer<Dog> = animalProducer;A producer of Animal could return an Animal that has no bark method. Therefore, TypeScript does not allow it to be used as a producer of Dog.
Reading the direction
When checking covariance, follow the direction of the value:
Dogcan be assigned toAnimal.Producer<Dog>can be assigned toProducer<Animal>.- The type parameter appears only where values leave the generic type.
- The assignment direction is preserved.
The out modifier documents this intention and asks TypeScript to check that T is used only in output positions. It does not convert an input type into a subtype or change the meaning of Dog and Animal.
Why producers are safe
Suppose a function accepts Producer<Animal>:
function describe(producer: Producer<Animal>) {
const animal = producer.get();
console.log(animal.name);
}The function uses only members guaranteed by Animal. Passing a Producer<Dog> is safe because every Dog has those Animal members. The function does not need to know that the producer returns a more specific type.
This pattern appears in read-only collections, data loaders, event result types, and APIs that return a specialized implementation through a general interface.
A type stops being purely covariant when its type parameter is also used as an input. For example, a type with both get(): T and set(value: T) must account for writes as well as reads. That situation requires a different variance analysis.
Exercise
If Cat extends Animal, which assignment is valid: Producer<Cat> to Producer<Animal>, or Producer<Animal> to Producer<Cat>? Why?
Show answer
Producer<Cat> to Producer<Animal> is valid. A producer that returns a Cat always returns an Animal, but an Animal producer might return a value that is not a Cat.