Sub topic 11
Declaring Invariance with `in out`
Prerequisites
This chapter assumes that you understand TypeScript generics, interfaces, structural typing, and assignability. You should also know that strict: true enables stricter function-parameter checking.
Variance describes how a relationship between type arguments affects a relationship between generic types. Suppose Dog extends Animal. A Dog can be used where an Animal is expected:
class Animal {
name = "animal";
}
class Dog extends Animal {
bark() {
return "woof";
}
}
const dog: Dog = new Dog();
const animal: Animal = dog; // OKThat relationship does not automatically make every generic type interchangeable. A generic type can produce values, consume values, or do both.
Why reading and writing require invariance
Consider a mutable box. It can return a value and accept a new value:
interface Box<in out T> {
get(): T;
set(value: T): void;
}
declare const dogBox: Box<Dog>;
declare const animalBox: Box<Animal>;
// const animals: Box<Animal> = dogBox; // Error
// const dogs: Box<Dog> = animalBox; // ErrorThe in out annotation declares T invariant. Neither Box<Dog> nor Box<Animal> can be assigned to the other.
Allowing Box<Dog> to become Box<Animal> would make this unsafe:
declare const dogBox2: Box<Dog>;
// const animalBox2: Box<Animal> = dogBox2;
// animalBox2.set(new Animal()); // This would put a non-Dog in a Dog boxAllowing the reverse assignment would also be unsafe. A box that accepts every Animal might contain a cat, but code using Box<Dog> could assume that every returned value is a Dog.
How `in out` differs from `in` and `out`
An output-only type is covariant. A producer of dogs can be used as a producer of animals:
interface Producer<out T> {
get(): T;
}
declare const dogs: Producer<Dog>;
const animals: Producer<Animal> = dogs; // OKAn input-only type is contravariant. A consumer that accepts every animal can also accept dogs:
interface Consumer<in T> {
accept(value: T): void;
}
declare const animalConsumer: Consumer<Animal>;
const dogConsumer: Consumer<Dog> = animalConsumer; // OKA box both produces and consumes T, so the safe relationship is invariant:
flowchart TD
D[Box of Dog] -->|cannot assign| A[Box of Animal]
A -->|cannot assign| D
P[Producer of Dog] -->|can assign to| PA[Producer of Animal]
C[Consumer of Animal] -->|can assign to| CD[Consumer of Dog]caption: Assignment directions for covariant, contravariant, and invariant generic types alt: A diagram showing that producer assignment follows the subtype direction, consumer assignment reverses it, and box assignment is blocked in both directions.
What the compiler checks
TypeScript checks whether the annotation matches the type parameter's use. This declaration is invalid because T is marked as output-only but appears as a function input:
interface Invalid<out T> {
write: (value: T) => void; // Error
}Changing the annotation to in out would accurately describe a type that both reads and writes T.
Variance annotations are mainly useful on interfaces and type aliases. They should document an intended public relationship, clarify recursive types, or improve difficult type comparisons. They do not change the behavior of ordinary values, and they cannot make an unsafe design safe.
Exercise
Why does Box<in out Dog> not assign to Box<in out Animal> when Dog extends Animal?
Show answer
The box both returns Dog values and accepts Dog values. Treating it as a box of Animal would allow a non-Dog Animal to be written into it, so TypeScript keeps the type parameter invariant.