Complete the code to declare a generic interface with a covariant type parameter.
interface Box<[1] T> {
value: T;
}The out keyword marks the type parameter as covariant, allowing safe output usage.
Complete the code to declare a generic interface with a contravariant type parameter.
interface Consumer<[1] T> {
consume(item: T): void;
}The in keyword marks the type parameter as contravariant, allowing safe input usage.
Fix the error in the function signature to correctly use variance with generic types.
function copy<[1] T>(source: ReadonlyArray<T>, destination: Array<T>): void { for (const item of source) { destination.push(item); } }
The out keyword allows T to be used covariantly in the source array.
Fill both blanks to create a generic interface with one covariant and one contravariant type parameter.
interface Transformer<[1] Input, [2] Output> { transform(input: Input): Output; }
Input is contravariant (marked with in) because it is used as a function parameter.Output is covariant (marked with out) because it is the return type.
Fill all three blanks to define a generic function with correct variance annotations for parameters and return type.
function compose<[1] A, [2] B, [3] C>( f: (input: A) => B, g: (input: B) => C ): (input: A) => C { return (input: A) => g(f(input)); }
A is contravariant because it is input to f.B is covariant because it is output of f and input to g.C is covariant because it is output of g and the final return type.