TypeScript 4.7 introduced explicit variance annotations 'in' and 'out' for generic type parameters. 'out T' marks a covariant position (the type only appears in outputs), 'in T' marks contravariance (only inputs), and 'in out' enforces invariance. These annotations are validated by the compiler: if you mark a parameter 'out' but use it in an input position, TypeScript will error. Beyond documentation, they speed up assignability checks because the compiler can short-circuit variance computation. Use them on interfaces whose generic parameter has a fixed directional role to make subtyping intent explicit and catch accidental mis-uses.
interface Producer<out T> {produce(): T;}interface Consumer<in T> {consume(value: T): void;}declare const animalProducer: Producer<{ name: string }>;// Covariant: a Producer<NarrowerOutput> flows into Producer<WiderOutput>const detailedProducer: Producer<{ name: string; age?: number }> = animalProducer;declare const stringConsumer: Consumer<string>;// Contravariant: a Consumer<WiderInput> flows into Consumer<NarrowerInput>const literalConsumer: Consumer<"hello"> = stringConsumer;