Sub topic 13
When Variance Annotations Help
Assume you understand TypeScript generics, interfaces, structural typing, assignability, and the strict compiler option. This chapter focuses on when to write in, out, or in out, rather than on how variance works from the beginning.
Variance annotations communicate the intended relationship between a generic type and its type parameter. They are most useful when a type is part of a public API, has a recursive definition, or causes expensive type checking.
Documenting API intent
An annotation makes the role of a type parameter visible to someone reading the declaration:
interface Producer<out T> {
get(): T;
}
interface Consumer<in T> {
accept(value: T): void;
}The annotations tell readers that Producer<T> produces values of type T, while Consumer<T> accepts values of type T. This intent remains clear even when the type has more members or several levels of nesting.
Annotations also help reviewers detect accidental changes. If someone later adds an input position to Producer<out T>, the compiler reports that the declaration no longer matches its annotation.
Recursive types
Recursive types refer to themselves. The compiler may need to compare the same generic structure repeatedly while deciding whether two instantiations are assignable.
A covariant annotation can make the intended relationship explicit in a recursive structure:
type Tree<out T> = {
value: T;
children: ReadonlyArray<Tree<T>>;
};
const numberTree: Tree<number> = {
value: 42,
children: []
};
const widerTree: Tree<number | string> = numberTree;
console.log(widerTree.value);Tree<number> can be used as Tree<number | string> because every number is also a number | string. The recursive children property preserves the same relationship at every level.
The annotation does not make an incorrectly designed recursive type safe. If Tree<T> also allowed writing an arbitrary T, then out T would contradict the structure and TypeScript would reject the declaration.
Improving checker performance
Variance annotations can improve performance during instantiation-based comparisons. These comparisons ask whether Producer<Dog> is assignable to Producer<Animal> and can use the declared variance without comparing every nested member.
This matters most for large libraries, deeply nested generic types, and recursive types. It usually does not matter for a small application.
Choosing deliberately
| Situation | Recommendation | Reason |
|---|---|---|
| Public producer or consumer type | Add `out` or `in` | Makes the contract readable |
| Deeply recursive generic type | Consider an annotation | Clarifies recursion and may reduce comparison work |
| Small private type | Usually omit it | Inference is simpler and performance is rarely an issue |
| Type whose parameter both reads and writes | Use `in out` if appropriate | Documents invariance and checks future edits |
TypeScript only allows variance annotations where a type parameter is declared, such as on an interface, class, or type alias. You cannot add out to an individual function parameter declaration. An annotation also does not change the underlying structure during every kind of comparison, so it cannot replace sound type design.
Exercise
You maintain a private, non-recursive Producer<T> used in one file. Should you add out T immediately?
Show answer
Usually no. Let TypeScript infer the variance unless the annotation documents an important contract or solves a measured checker-performance problem.