Complete the code to declare a class that implements an interface.
interface Person {
name: string;
age: number;
}
class Employee implements [1] {
constructor(public name: string, public age: number) {}
}The class Employee implements the interface Person to ensure it has the required properties.
Complete the code to merge a class and an interface with the same name.
interface User {
id: number;
}
class [1] {
constructor(public id: number, public name: string) {}
}In TypeScript, a class and an interface with the same name merge. Here, the class User merges with the interface User.
Fix the error in the merged class and interface by completing the code.
interface Product {
price: number;
}
class Product {
constructor(public price: number, public name: string) {}
getPrice() {
return this.[1];
}
}The method getPrice should return the price property defined in the class and interface.
Fill both blanks to add a method to the interface and implement it in the class.
interface Vehicle {
speed: number;
[1](): string;
}
class Vehicle {
constructor(public speed: number) {}
[2]() {
return `Speed is ${this.speed} km/h`;
}
}The interface declares a method getSpeed() and the class implements it as getSpeed().
Fill all three blanks to merge an interface and class, adding a property and method.
interface Book {
title: string;
[1](): string;
}
class Book {
constructor(public title: string, public author: string) {}
[2]() {
return `${this.title} by ${this.author}`;
}
[3]: number = 2024;
}The interface declares a method getDetails(). The class implements it as getDetails and adds a property publishedYear.