0
0
Typescriptprogramming~20 mins

Why inheritance needs types in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inheritance Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method call with inheritance and types
What is the output of this TypeScript code that uses inheritance and typed methods?
Typescript
class Animal {
  speak(): string {
    return "Generic sound";
  }
}

class Dog extends Animal {
  speak(): string {
    return "Woof!";
  }
}

const pet: Animal = new Dog();
console.log(pet.speak());
A"Woof!"
BTypeError at runtime
Cundefined
D"Generic sound"
Attempts:
2 left
💡 Hint
Remember that the method called depends on the actual object type, not the variable type.
🧠 Conceptual
intermediate
1:30remaining
Why does TypeScript require types in inheritance?
Why does TypeScript require explicit types when using inheritance?
ATo allow inheritance only between classes with the same name
BTo make the code run faster in the browser
CTo ensure methods and properties are correctly checked and prevent runtime errors
DTo automatically convert all types to strings
Attempts:
2 left
💡 Hint
Think about how types help catch mistakes before running the code.
🔧 Debug
advanced
2:30remaining
Identify the type error in this inheritance example
What type error does this TypeScript code produce?
Typescript
class Vehicle {
  speed: number;
  constructor(speed: number) {
    this.speed = speed;
  }
}

class Car extends Vehicle {
  speed: string;
  constructor(speed: string) {
    super(0);
    this.speed = speed;
  }
}

const myCar = new Car("fast");
AMissing return type on constructor
BNo error, code compiles fine
CCannot call super() without arguments
DProperty 'speed' in 'Car' conflicts with inherited property of type 'number'
Attempts:
2 left
💡 Hint
Check the types of the 'speed' property in both classes.
📝 Syntax
advanced
2:00remaining
Which option correctly types a subclass constructor?
Which constructor syntax correctly types the subclass in TypeScript?
Typescript
class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}

class Employee extends Person {
  employeeId: number;
  // constructor here
}
A
constructor(name: string) {
  super(name);
  this.employeeId = 0;
}
B
constructor(name: string, employeeId: number) {
  super(name);
  this.employeeId = employeeId;
}
C
constructor(name: string, employeeId: string) {
  super(name);
  this.employeeId = employeeId;
}
D
constructor(employeeId: number) {
  this.employeeId = employeeId;
  super('Unknown');
}
Attempts:
2 left
💡 Hint
Remember that super() must be called before using 'this' and types must match.
🚀 Application
expert
2:30remaining
Determine the number of properties in the final object
Given this TypeScript inheritance code, how many own properties does the instance 'obj' have?
Typescript
class Base {
  baseProp = 1;
}

class Derived extends Base {
  derivedProp = 2;
}

const obj = new Derived();
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Count the properties assigned directly on the instance, not inherited ones.