TypeScript Generics and Advanced Type Operators
in variance annotation — contravariant parameters
A contravariant parameter reverses the usual direction of assignability between instantiations of the same generic type: a consumer of a wider type can serve as a consumer of a narrower type. TypeScript normally infers variance. The in annotation describes that relationship and is checked against certain incompatible declarations. It is not a way to force arbitrary structural comparisons or runtime input validation. Function-valued properties in strict function checking avoid the method bivariance distinction. Example output: "ready". Local strict TypeScript 5.8.3 / ES2022 baseline. The type construct itself is erased; the logged value illustrates a checked use, not runtime type validation.
Formal Syntax & Structure
typescript syntaxtypescript
type Consumer<in T> = { accept: (value: T) => void };Working Example
Example Snippettypescript
type Consumer<in T> = { accept: (value: T) => void };const all: Consumer<unknown> = { accept: value => console.log(String(value)) };const text: Consumer<string> = all;text.accept("ready");