TypeScript · Senior · Question 23 of 75

What are the in and out variance annotations on type parameters (TS 4.7) for?

interface Producer<out T> { get(): T }
interface Consumer<in T> { set(v: T): void }

Explanation

TS usually computes variance itself, but for complex recursive types this is expensive and sometimes imprecise. The annotation is checked by the compiler and used in comparisons. It's rarely needed - in libraries with complex types.

Further reading

Problem with a question

TypeScript · Senior · question 23

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz