Sub topic 12
What Happens When an Annotation Contradicts the Type
Assume you know TypeScript generics, structural typing, assignability, and the strict compiler option. This chapter focuses on what the compiler does when in, out, or in out disagrees with how a type parameter is used.
A variance annotation is a declaration of intent. It does not act as a cast. The compiler checks whether the type's structure supports the annotation.
An `out` annotation must describe outputs
A type parameter marked out must be used only in output positions. A return type is an output position because a caller receives that value.
The parameter of a function is an input position. Therefore, this declaration contradicts its own annotation:
// Uncomment this declaration to see a variance error.
// interface Writer<out T> {
// write: (value: T) => void;
// }T appears as the parameter of the function stored in write, so T is consumed rather than produced. The compiler rejects the declaration instead of accepting a misleading variance relationship.
The corrected declaration is contravariant:
interface Writer<in T> {
write: (value: T) => void;
}
declare const animalWriter: Writer<Animal>;
declare const dogWriter: Writer<Dog>;
const acceptsDogs: Writer<Dog> = animalWriter;An Animal writer can safely receive a Dog, so this assignment is valid. The reverse assignment would be unsafe because a writer that accepts only dogs cannot necessarily handle every animal.
An `in` annotation must describe inputs
The reverse contradiction occurs when in appears on a type parameter that is returned:
// Uncomment this declaration to see a variance error.
// interface BadReader<in T> {
// get: () => T;
// }The get function produces T, so the type parameter is covariant, not contravariant. The appropriate annotation is out:
interface Reader<out T> {
get: () => T;
}The compiler checks these uses when it checks the declaration. This catches errors close to their source, rather than allowing a false relationship to spread through many assignments.
| Annotation | Allowed direction of use | Typical example |
|---|---|---|
| `out T` | Produce `T` | `get(): T` |
| `in T` | Consume `T` | `set(value: T): void` |
| `in out T` | Produce and consume `T` | `get(): T` and `set(value: T)` |
Use `in out` for both directions
A mutable container usually reads and writes its type parameter. Such a type is invariant:
interface Box<in out T> {
get(): T;
set(value: T): void;
}
declare const dogBox: Box<Dog>;
// Neither assignment is valid:
// const animalBox: Box<Animal> = dogBox;
// const anotherDogBox: Box<Dog> = animalBox;If Box<Dog> could become Box<Animal>, code could insert an ordinary Animal into a box that promises to contain dogs. If the reverse assignment were allowed, code could read an animal as though it were always a dog.
Reading the compiler's diagnostic
When an annotation contradicts the structure, TypeScript reports the declaration as inconsistent with its variance annotation. Check each occurrence of the type parameter:
Remember that function parameters reverse the direction of variance. With strictFunctionTypes, function-typed properties receive strict checking. Method declarations have special historical bivariance behavior, so prefer function-typed properties when you want the type's input and output behavior to be explicit.
Exercise
Why does interface Bad<out T> { write: (value: T) => void } produce a variance error?
Show answer
T is used as a function input, but out promises that T is used only as an output. Use in T, or change the structure so it produces T.