0
0
Typescriptprogramming~20 mins

Extending classes with types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Extending Classes with Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code extending a class with a generic type?

Consider the following TypeScript code where a class Box is extended by ColoredBox with a generic type. What will be printed when the code runs?

Typescript
class Box<T> {
  content: T;
  constructor(content: T) {
    this.content = content;
  }
  getContent(): T {
    return this.content;
  }
}

class ColoredBox<T> extends Box<T> {
  color: string;
  constructor(content: T, color: string) {
    super(content);
    this.color = color;
  }
  describe(): string {
    return `Box of color ${this.color} containing: ${this.content}`;
  }
}

const box = new ColoredBox<number>(42, "red");
console.log(box.describe());
ABox of color red containing: 42
BTypeError at runtime
CBox of color undefined containing: 42
DBox of color red containing: [object Object]
Attempts:
2 left
💡 Hint

Look at how the ColoredBox class calls super(content) and how content is used in the describe method.

Predict Output
intermediate
2:00remaining
What is the value of result after running this TypeScript code with class extension and type constraints?

Given the following TypeScript code, what is the value of result after execution?

Typescript
class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

class Dog extends Animal {
  breed: string;
  constructor(name: string, breed: string) {
    super(name);
    this.breed = breed;
  }
}

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

const dog = new Dog("Buddy", "Beagle");
const result = getAnimalName(dog);
Aundefined
B"Beagle"
C"Buddy"
DTypeError at runtime
Attempts:
2 left
💡 Hint

Remember that getAnimalName returns the name property from the Animal class.

🔧 Debug
advanced
2:00remaining
What error does this TypeScript code produce when extending a class with a type mismatch?

Examine the following TypeScript code. What error will the TypeScript compiler report?

Typescript
class Container<T> {
  value: T;
  constructor(value: T) {
    this.value = value;
  }
}

class NumberContainer extends Container<number> {
  constructor(value: string) {
    super(value);
  }
}

const numCont = new NumberContainer("123");
ANo error, code compiles successfully.
BArgument of type 'string' is not assignable to parameter of type 'number'.
CProperty 'value' does not exist on type 'NumberContainer'.
DType 'NumberContainer' is not assignable to type 'Container<number>'.
Attempts:
2 left
💡 Hint

Check the constructor parameter types and what is passed to super().

📝 Syntax
advanced
2:00remaining
Which option correctly extends a generic class with an additional property in TypeScript?

Choose the correct TypeScript code that extends a generic class Pair<T, U> by adding a new property label of type string.

A
class LabeledPair&lt;T, U&gt; extends Pair&lt;T, U&gt; {
  label: string;
  constructor(first: T, second: U, label: number) {
    super(first, second);
    this.label = label;
  }
}
B
class LabeledPair&lt;T, U&gt; extends Pair&lt;T, U&gt; {
  label: string;
  constructor(label: string) {
    super();
    this.label = label;
  }
}
C
class LabeledPair&lt;T, U&gt; extends Pair&lt;T, U&gt; {
  label: string;
  constructor(first: T, second: U) {
    super(first, second);
    this.label = label;
  }
}
D
class LabeledPair&lt;T, U&gt; extends Pair&lt;T, U&gt; {
  label: string;
  constructor(first: T, second: U, label: string) {
    super(first, second);
    this.label = label;
  }
}
Attempts:
2 left
💡 Hint

Check the constructor parameters and types carefully, especially the label property type.

🚀 Application
expert
2:00remaining
How many properties does an instance of this extended class have at runtime?

Given the following TypeScript code, how many own properties does the instance obj have at runtime?

Typescript
class Base<T> {
  data: T;
  constructor(data: T) {
    this.data = data;
  }
}

class Extended extends Base<number> {
  extra: string;
  constructor(data: number, extra: string) {
    super(data);
    this.extra = extra;
  }
}

const obj = new Extended(10, "hello");
A2
B1
C0
D3
Attempts:
2 left
💡 Hint

Count the properties assigned directly on the instance in the constructors.