Bird
Raised Fist0
Typescriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What happens when you declare an interface with the same name as a class in TypeScript?

For example:
class Car { drive() {} }
interface Car { color: string; }

easy
A. The interface replaces the class completely.
B. The interface merges with the class, adding the 'color' property to the class type.
C. TypeScript throws a syntax error because names conflict.
D. The class ignores the interface and remains unchanged.

Solution

  1. Step 1: Understand interface and class merging

    In TypeScript, if an interface has the same name as a class, their types merge. This means the class type now includes members from both.
  2. Step 2: Apply to example

    The interface adds a 'color' property to the class type, so instances of Car are expected to have 'drive()' and 'color'.
  3. Final Answer:

    The interface merges with the class, adding the 'color' property to the class type. -> Option B
  4. Quick Check:

    Same name interface + class = merged type [OK]
Hint: Same name means interface merges into class type [OK]
Common Mistakes:
  • Thinking interface replaces class
  • Expecting a syntax error
  • Assuming class ignores interface
2.

Which of the following is the correct syntax to merge an interface with a class named Person in TypeScript?

easy
A. interface Person { age: number; }
B. interface Person() { age: number; }
C. interface Person[] { age: number; }
D. interface Person = { age: number; }

Solution

  1. Step 1: Recall interface declaration syntax

    Interfaces are declared with the keyword 'interface' followed by the name and curly braces containing members.
  2. Step 2: Check each option

    interface Person { age: number; } uses correct syntax: 'interface Person { age: number; }'. Options A, C, and D use invalid syntax like parentheses, brackets, or equals sign.
  3. Final Answer:

    interface Person { age: number; } -> Option A
  4. Quick Check:

    Correct interface syntax = interface Person { age: number; } [OK]
Hint: Interfaces use curly braces, no parentheses or equals [OK]
Common Mistakes:
  • Using parentheses after interface name
  • Using brackets instead of braces
  • Assigning interface with equals sign
3.

Given the following code, what is the output?

class Animal {
  speak() { return "Roar"; }
}
interface Animal {
  age: number;
}

const a: Animal = { age: 5, speak() { return "Roar"; } };
console.log(a.age, a.speak());
medium
A. Runtime error
B. undefined Roar
C. 5 undefined
D. 5 Roar

Solution

  1. Step 1: Understand merging and object literal

    The interface adds 'age' to Animal type. The object 'a' has both 'age' and 'speak' method matching Animal type.
  2. Step 2: Check console.log output

    Logging 'a.age' prints 5, and 'a.speak()' returns "Roar". So output is '5 Roar'.
  3. Final Answer:

    5 Roar -> Option D
  4. Quick Check:

    Object has both properties, prints values [OK]
Hint: Merged type means object must have all members [OK]
Common Mistakes:
  • Expecting undefined for age
  • Confusing method call with property
  • Thinking runtime error occurs
4.

Identify the error in this code that merges a class and interface named Book:

class Book {
  title: string;
  constructor(title: string) {
    this.title = title;
  }
}
interface Book {
  author: string;
}

const b = new Book("1984");
console.log(b.author);
medium
A. Constructor syntax is invalid.
B. Class and interface names must differ to merge.
C. Property 'author' is missing in class implementation causing runtime error.
D. No error; code runs fine.

Solution

  1. Step 1: Check merging requirements

    The interface adds 'author' property to Book type, but the class does not implement it or assign it.
  2. Step 2: Analyze runtime behavior

    Creating 'b' with new Book("1984") sets 'title' but 'author' is undefined. Accessing 'b.author' prints undefined, which may cause issues if expected to exist.
  3. Final Answer:

    Property 'author' is missing in class implementation causing runtime error. -> Option C
  4. Quick Check:

    Interface adds property, class must implement it [OK]
Hint: Merged interface members must be implemented in class [OK]
Common Mistakes:
  • Thinking names must differ
  • Ignoring missing property implementation
  • Assuming no error occurs
5.

You want to extend a class Employee by merging an interface with the same name to add a new method getSalary(). How should you correctly implement this so that getSalary() is usable on Employee instances?

class Employee {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
}
interface Employee {
  getSalary(): number;
}

// What is the correct way to implement getSalary?
hard
A. Add getSalary() method to Employee.prototype after class declaration.
B. Add getSalary() method only in the interface; no class change needed.
C. Add getSalary() as a standalone function outside the class.
D. Add getSalary() method inside the Employee class body.

Solution

  1. Step 1: Understand interface merging with class

    Merging adds type info but does not add implementation. The class must implement the method or it will cause errors.
  2. Step 2: Implement method correctly

    Since the class does not have getSalary(), adding it to Employee.prototype after declaration provides the implementation and satisfies the interface.
  3. Final Answer:

    Add getSalary() method to Employee.prototype after class declaration. -> Option A
  4. Quick Check:

    Interface adds type; method must be implemented in class or prototype [OK]
Hint: Implement interface methods in class or prototype [OK]
Common Mistakes:
  • Adding method only in interface without implementation
  • Trying to add method outside prototype
  • Assuming interface adds implementation automatically
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard