Challenge - 5 Problems
Generic Type Variance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
Think about how the generic type T extends Animal and what property is accessed.
✗ Incorrect
The function getName accepts any type T that extends Animal, so Dog is valid. It returns the name property, which exists on Dog. So the output is the dog's name, "Buddy".
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
Consider function parameter type compatibility and contravariance.
✗ Incorrect
In TypeScript, function parameter types are contravariant, so assigning feedAnimal (which accepts Animal) to feedDog (which expects Dog) is allowed. The function runs and returns "Feeding Max".
🔧 Debug
advanced2: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;
Attempts:
2 left
💡 Hint
Think about how generic types behave when assigned to each other.
✗ Incorrect
Generic types like Box are invariant by default in TypeScript. So Box cannot be assigned to Box because it could break type safety.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
TypeScript does not support 'in' or 'super' keywords for generics like some other languages.
✗ Incorrect
TypeScript uses 'extends' to constrain generic types. It does not have 'in' or 'super' keywords for variance. So only option D is valid syntax.
🚀 Application
expert3: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);Attempts:
2 left
💡 Hint
Consider function parameter contravariance and return type covariance in TypeScript.
✗ Incorrect
Assigning handler1 (which accepts Animal) to func1 (which expects a function accepting Dog) is allowed due to contravariance in parameters. Calling func1(dog) calls handler1 with dog, returning "Animal: Rex".