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 }- ✓Correct answer: They explicitly set covariance/contravariance, speeding up and sharpening checks
- 2They define which parameters can be read and written at runtime
- 3It's syntax for input and output parameters, as in C# methods
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.