0
0
Typescriptprogramming~20 mins

Generic type variance in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Type Variance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of generic function with covariance
What is the output of this TypeScript code when run with console.log(result);?
Typescript
interface Animal { name: string }
interface Dog extends Animal { bark(): void }

function getName<T extends Animal>(animal: T): string {
  return animal.name;
}

const dog: Dog = { name: "Buddy", bark: () => console.log("Woof!") };
const result = getName(dog);
A"Buddy"
Bundefined
CTypeError at runtime
DCompilation error
Attempts:
2 left
💡 Hint
Think about how the generic type T extends Animal and what property is accessed.
Predict Output
intermediate
2:00remaining
Effect of contravariance in function parameters
What will this TypeScript code output when executed?
Typescript
interface Animal { name: string }
interface Dog extends Animal { bark(): void }

function feedAnimal(animal: Animal) {
  return `Feeding ${animal.name}`;
}

const feedDog: (dog: Dog) => string = feedAnimal;
const dog: Dog = { name: "Max", bark: () => console.log("Woof!") };
const result = feedDog(dog);
console.log(result);
A"Feeding Max"
BTypeError at runtime
CCompilation error due to type incompatibility
Dundefined
Attempts:
2 left
💡 Hint
Consider function parameter type compatibility and contravariance.
🔧 Debug
advanced
2:30remaining
Identify the variance error in generic interface assignment
Which option correctly explains the error in this TypeScript code snippet?
Typescript
interface Box<T> {
  value: T;
}

let animalBox: Box<Animal> = { value: { name: "Leo" } };
let dogBox: Box<Dog> = animalBox;
AError because Dog is not assignable to Animal
BNo error, assignment is valid due to covariance
CError because Box<T> is invariant and Box<Animal> cannot be assigned to Box<Dog>
DError because Box<T> is contravariant
Attempts:
2 left
💡 Hint
Think about how generic types behave when assigned to each other.
📝 Syntax
advanced
2:00remaining
Which generic function signature is valid for contravariant parameter?
Which option shows a valid TypeScript generic function signature that enforces contravariance on the parameter type?
Afunction process<T>(input: T): void
Bfunction process<in T>(input: T): void
Cfunction process<T super Animal>(input: T): void
Dfunction process<T extends Animal>(input: T): void
Attempts:
2 left
💡 Hint
TypeScript does not support 'in' or 'super' keywords for generics like some other languages.
🚀 Application
expert
3:00remaining
Determine the output of complex generic variance with function types
Given the following TypeScript code, what is the output when console.log(result); runs?
Typescript
interface Animal { name: string }
interface Dog extends Animal { bark(): void }

function handler1(animal: Animal): string {
  return `Animal: ${animal.name}`;
}

function handler2(dog: Dog): string {
  return `Dog: ${dog.name}`;
}

let func1: (dog: Dog) => string = handler1;
let dog: Dog = { name: "Rex", bark: () => console.log("Woof!") };
let result = func1(dog);
ATypeError at runtime
B"Animal: Rex"
CCompilation error due to incompatible function types
D"Dog: Rex"
Attempts:
2 left
💡 Hint
Consider function parameter contravariance and return type covariance in TypeScript.