0
0
Typescriptprogramming~20 mins

Merging classes with interfaces in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Merging Classes with Interfaces
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of merged class and interface properties
What is the output of this TypeScript code when creating an instance of Person and accessing person.greet() and person.age?
Typescript
interface Person {
  age: number;
  greet(): string;
}

class Person {
  constructor(public age: number) {}
  greet() {
    return `Hello, I am ${this.age} years old.`;
  }
}

const person = new Person(30);
console.log(person.greet());
console.log(person.age);
ASyntaxError: Duplicate identifier 'Person'
B
"Hello, I am undefined years old."
undefined
C
"Hello, I am 30 years old."
30
DTypeError: person.greet is not a function
Attempts:
2 left
💡 Hint
Remember that in TypeScript, interfaces can merge with classes to add type information, but the class implementation defines the actual behavior.
Predict Output
intermediate
1:30remaining
Property count after merging class and interface
Given this TypeScript code, how many own properties does the employee object have after creation?
Typescript
interface Employee {
  id: number;
  department: string;
}

class Employee {
  constructor(public id: number) {}
  department = "Sales";
}

const employee = new Employee(101);
console.log(Object.keys(employee).length);
A0
B1
CThrows a runtime error
D2
Attempts:
2 left
💡 Hint
Check which properties are assigned directly on the instance.
🔧 Debug
advanced
2:30remaining
Why does this merged class and interface code cause an error?
This TypeScript code tries to merge a class and interface named Car. Why does it cause a compilation error?
Typescript
interface Car {
  speed: number;
  drive(): void;
}

class Car {
  speed: string;
  constructor(speed: string) {
    this.speed = speed;
  }
  drive() {
    console.log(`Driving at ${this.speed}`);
  }
}
ATypeScript error because interface and class have conflicting types for 'speed' property
BRuntime error because 'drive' method is missing
CSyntax error due to duplicate class declaration
DNo error, code compiles and runs fine
Attempts:
2 left
💡 Hint
Check the types of the 'speed' property in both interface and class.
📝 Syntax
advanced
2:30remaining
Which option correctly merges a class and interface with method overloads?
You want to merge a class and interface named Calculator where the interface declares two overloads for calculate. Which option correctly compiles?
A
interface Calculator {
  calculate(x: number): number;
  calculate(x: string): string;
}
class Calculator {
  calculate(x: number): number {
    return x * 2;
  }
  calculate(x: string): string {
    return x + x;
  }
}
B
interface Calculator {
  calculate(x: number): number;
  calculate(x: string): string;
}
class Calculator {
  calculate(x: number | string): number | string {
    if (typeof x === 'number') return x * 2;
    else return x + x;
  }
}
C
interface Calculator {
  calculate(x: number): number;
  calculate(x: string): string;
}
class Calculator {
  calculate(x: any): any {
    if (typeof x === 'number') return x * 2;
    else return x + x;
  }
}
D
interface Calculator {
  calculate(x: number): number;
  calculate(x: string): string;
}
class Calculator {
  calculate(x: number | string): number {
    return x * 2;
  }
}
Attempts:
2 left
💡 Hint
In TypeScript, method overloads are declared in the interface, but the class must implement a single method with a compatible signature.
🚀 Application
expert
3:00remaining
How many properties does this merged class/interface instance have?
Consider this TypeScript code merging a class and interface named Widget. After creating const w = new Widget();, how many own enumerable properties does w have?
Typescript
interface Widget {
  name: string;
  size: number;
  render(): void;
}

class Widget {
  name = "MyWidget";
  private size = 10;
  render() {
    console.log(`Rendering ${this.name}`);
  }
}

const w = new Widget();
console.log(Object.keys(w).length);
A1
B2
C0
D3
Attempts:
2 left
💡 Hint
Remember that private properties are not enumerable and do not appear in Object.keys output.