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
intermediate2: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);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.
✗ Incorrect
The interface
Person declares the shape with age and greet(). The class Person implements these members. When merged, the class provides the implementation and the interface provides the type. So, person.greet() returns the greeting string and person.age is 30.❓ Predict Output
intermediate1: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);Attempts:
2 left
💡 Hint
Check which properties are assigned directly on the instance.
✗ Incorrect
The class constructor assigns
id as a public property, and department is initialized as a class field. Both become own properties of the employee object. So Object.keys(employee) returns ["id", "department"] which has length 2.🔧 Debug
advanced2: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}`);
}
}Attempts:
2 left
💡 Hint
Check the types of the 'speed' property in both interface and class.
✗ Incorrect
The interface
Car declares speed as a number, but the class declares it as a string. This type conflict causes a TypeScript compilation error because merged declarations must be compatible.📝 Syntax
advanced2: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?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.
✗ Incorrect
Option B correctly declares overloads in the interface and implements a single method in the class with a union type parameter and union return type. Option B tries to implement two methods with the same name, which is invalid. Option B lacks parameter types. Option B returns number but does not handle string input properly.
🚀 Application
expert3: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);Attempts:
2 left
💡 Hint
Remember that private properties are not enumerable and do not appear in Object.keys output.
✗ Incorrect
The class
Widget has a public property name initialized as a class field, which is enumerable and own property. The size property is private and thus not enumerable. The method render is on the prototype, not an own property. So Object.keys(w) returns ["name"] with length 1.