Complete the code to declare a covariant array of strings.
let fruits: ReadonlyArray<string> = ['apple', 'banana', 'cherry']; let moreFruits: ReadonlyArray<[1]> = fruits;
The ReadonlyArray<string> type is covariant, so assigning fruits to moreFruits of the same type string works.
Complete the function type to accept a parameter contravariantly.
type Handler = (input: [1]) => void;
let handleString: Handler = (text: string) => console.log(text);string makes the parameter invariant, not contravariant.number or unknown causes type errors.Function parameters are contravariant, so a handler accepting any can replace one expecting string.
Fix the error in the assignment by choosing the correct type for the function parameter.
type Func = (arg: [1]) => void;
let funcString: Func = (s: string) => console.log(s);
let funcAny: Func = funcString;string causes a type error.number or unknown is incorrect here.Assigning a function with parameter string to one expecting any is invalid because parameters are contravariant. The parameter type must be more general (any) to accept the assignment.
Fill both blanks to create a covariant return type and contravariant parameter type in the function signature.
type Transformer = (input: [1]) => [2]; let transform: Transformer = (value) => value.toString();
The parameter type is contravariant, so it should be more general (any). The return type is covariant, so it should be more specific (string).
Fill all three blanks to define a function type with contravariant parameter, covariant return, and assign it correctly.
type FuncType = (param: [1]) => [2]; let func: FuncType = (x: [3]) => x.toString();
The function type expects a parameter of type any (contravariant), returns a string (covariant), and the assigned function accepts a number parameter, which is allowed because number is assignable to any.