0
0
Typescriptprogramming~20 mins

Covariance and contravariance in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Covariance and Contravariance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Covariance in TypeScript arrays
What is the output of this TypeScript code snippet?
Typescript
class Animal { name: string = 'animal'; }
class Dog extends Animal { bark() { return 'woof'; } }

let dogs: Dog[] = [new Dog()];
let animals: Animal[] = dogs;
animals.push(new Animal());
console.log(dogs.length);
ACompilation error
B1
C0
D2
Attempts:
2 left
💡 Hint
Arrays in TypeScript are covariant, so assigning Dog[] to Animal[] is allowed.
Predict Output
intermediate
2:00remaining
Contravariance in function parameters
What is the output of this TypeScript code?
Typescript
class Animal { name = 'animal'; }
class Dog extends Animal { bark() { return 'woof'; } }

function feedAnimal(feed: (a: Animal) => void) {
  feed(new Animal());
}

function feedDog(dog: Dog) {
  console.log('Feeding dog:', dog.name);
}

feedAnimal(feedDog);
ACompilation error
BRuntime error
CFeeding dog: animal
DNo output
Attempts:
2 left
💡 Hint
Function parameter types are contravariant in TypeScript.
🔧 Debug
advanced
2:00remaining
Fix the type error with function assignment
Which option correctly fixes the type error in this code snippet?
Typescript
class Animal { name = 'animal'; }
class Dog extends Animal { bark() { return 'woof'; } }

let feedAnimal: (a: Animal) => void;
let feedDog: (d: Dog) => void = (d) => console.log(d.bark());

feedAnimal = feedDog; // Error here
AChange feedDog type to (d: Animal) => void
BChange feedAnimal type to (a: Dog) => void
CUse type assertion: feedAnimal = feedDog as (a: Animal) => void
DSwap the assignment: feedDog = feedAnimal
Attempts:
2 left
💡 Hint
Function parameter types must be contravariant for assignment.
Predict Output
advanced
2:00remaining
Covariance with readonly arrays
What is the output of this TypeScript code?
Typescript
class Animal { name = 'animal'; }
class Dog extends Animal { bark() { return 'woof'; } }

let dogs: ReadonlyArray<Dog> = [new Dog()];
let animals: ReadonlyArray<Animal> = dogs;
console.log(animals.length);
ACompilation error
B1
C0
DRuntime error
Attempts:
2 left
💡 Hint
ReadonlyArray is covariant in TypeScript.
Predict Output
expert
3:00remaining
Complex variance with generics and function types
What is the output of this TypeScript code?
Typescript
class Animal { name = 'animal'; }
class Dog extends Animal { bark() { return 'woof'; } }

interface Processor<T> {
  process: (input: T) => T;
}

let dogProcessor: Processor<Dog> = {
  process: (d) => { console.log(d.bark()); return d; }
};

let animalProcessor: Processor<Animal> = dogProcessor;

const result = animalProcessor.process(new Animal());
console.log(result.name);
Aanimal
Bwoof\nanimal
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Generic interfaces with function properties are invariant by default.