0
0
Typescriptprogramming~20 mins

Type compatibility with classes in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Compatibility Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of class instance assignment compatibility
Consider these two classes and the assignment below. What will be the output when running this code?
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;
  }
}

let animal: Animal = new Dog('Buddy', 'Golden Retriever');
console.log(animal.name);
AError: Property 'name' does not exist on type 'Animal'
Bundefined
C"Golden Retriever"
D"Buddy"
Attempts:
2 left
💡 Hint
Think about inheritance and what properties are available on the base class reference.
Predict Output
intermediate
2:00remaining
Assigning incompatible class instances
What error will this TypeScript code produce?
Typescript
class Car {
  wheels: number = 4;
}

class Boat {
  sails: number = 2;
}

let vehicle: Car = new Boat();
ANo error, assignment is allowed.
BType 'Boat' is not assignable to type 'Car'. Property 'wheels' is missing in type 'Boat'.
CRuntime error: Cannot read property 'wheels' of undefined.
DType 'Car' is not assignable to type 'Boat'.
Attempts:
2 left
💡 Hint
Check if the properties of the assigned object match the target type.
🔧 Debug
advanced
2:30remaining
Why does this class assignment cause an error?
Given these classes, why does the assignment cause a TypeScript error? Identify the reason.
Typescript
class Person {
  private id: number;
  constructor(id: number) {
    this.id = id;
  }
}

class Employee {
  private id: number;
  constructor(id: number) {
    this.id = id;
  }
}

let p: Person = new Employee(123);
ABecause private members with the same name but declared in different classes make the types incompatible.
BBecause the classes have different constructors.
CBecause TypeScript does not allow assignment between any classes.
DBecause the classes have different property types.
Attempts:
2 left
💡 Hint
Check how private members affect type compatibility in TypeScript.
📝 Syntax
advanced
2:00remaining
Which class assignment is valid in TypeScript?
Given these classes, which assignment will NOT cause a TypeScript error?
Typescript
class Alpha {
  x: number = 1;
}

class Beta extends Alpha {
  y: number = 2;
}

class Gamma {
  y: number = 2;
}

let a: Alpha;
let b: Beta;
let g: Gamma;
Aa = b;
Bb = a;
Cg = a;
Da = g;
Attempts:
2 left
💡 Hint
Remember that a subclass instance can be assigned to a base class variable.
🚀 Application
expert
3:00remaining
Predict the output of method compatibility in class assignment
What will be the output of this TypeScript code when executed?
Typescript
class Base {
  greet(): string {
    return "Hello from Base";
  }
}

class Derived {
  greet(): string {
    return "Hello from Derived";
  }
}

let base: Base = new Derived();
console.log(base.greet());
A"Hello from Base"
BType error: Type 'Derived' is not assignable to type 'Base'.
C"Hello from Derived"
DRuntime error: base.greet is not a function.
Attempts:
2 left
💡 Hint
Think about method overriding and how TypeScript handles structural typing for classes.