Sub topic 14
Where annotations are allowed and what they do not change
Assume you know TypeScript generics, interfaces, structural typing, and the strict compiler option. This chapter focuses on the limits of variance annotations. The annotations describe an intended relationship between type arguments. They do not create a new runtime feature.
Where variance annotations are allowed
TypeScript supports in, out, and in out on type parameters declared by interfaces and type aliases:
interface Producer<out T> {
get(): T;
}
type Consumer<in T> = (value: T) => void;
interface Box<in out T> {
get(): T;
set(value: T): void;
}The annotation must appear immediately before the type parameter name. out T documents covariance, in T documents contravariance, and in out T documents invariance.
Variance annotations are not allowed directly on classes or functions:
// Not allowed:
// class Producer<out T> {}
// Not allowed:
// function make<out T>(): T {}A class can still participate in variance through its structure. However, you cannot add a variance modifier to the class declaration. If a public class needs a documented variance boundary, expose an interface or type alias for that boundary.
flowchart TD
A[Generic type parameter] --> B{Declaration kind}
B -->|Interface| C[Variance annotation allowed]
B -->|Type alias| C
B -->|Class| D[Annotation not allowed]
B -->|Function| D
C --> E[Compiler checks the annotation]
E --> F[Annotation can guide generic comparisons]What annotations change
An annotation tells the compiler how to compare two instantiations of the same generic declaration.
| Annotation | Intended relationship | Example assignment |
|---|---|---|
| `out T` | Covariant, output only | `Producer<Dog>` to `Producer<Animal>` |
| `in T` | Contravariant, input only | `Consumer<Animal>` to `Consumer<Dog>` |
| `in out T` | Invariant, input and output | Neither direction for `Box<Dog>` and `Box<Animal>` |
The compiler checks that the declaration matches the annotation:
interface Animal {
name: string;
}
interface Dog extends Animal {
bark(): void;
}
interface Producer<out T> {
get(): T;
}
declare const dogs: Producer<Dog>;
const animals: Producer<Animal> = dogs;The annotation does not make unrelated types assignable. Dog must still be a subtype of Animal. It only states how that existing relationship should carry through Producer<T>.
What annotations do not change
Variance annotations do not change JavaScript output. They disappear during compilation.
They also do not change the meaning of the type's members. If T appears in an input position, writing out T does not safely turn that input into an output. Instead, TypeScript reports a contradiction:
interface BadWriter<out T> {
// Error: T is used in an input position.
write: (value: T) => void;
}An annotation does not disable structural typing. Two types are still compared by their members rather than by their names. It also does not make mutable data safe. For example, Dog[] remains subject to TypeScript's historical array behavior. Prefer ReadonlyArray<T> when a value should only be read.
When to omit an annotation
TypeScript usually infers variance from how a type parameter is used. Leave the annotation out when the declaration is small, obvious, and not part of a reusable public type boundary.
Add one when it documents an important API contract, clarifies a recursive type, or helps the compiler compare complicated generic types. Remember that annotations are only available on interfaces and type aliases, and they do not alter runtime behavior or the underlying member types.
Exercise
Can you write class Store<in out T> to declare an invariant class type parameter?
Show answer
No. Variance annotations are supported on interfaces and type aliases, not directly on class declarations. The class's structure still determines how it behaves in assignability checks.