I have created a unique custom class named GreetingsManager.
It includes a special method named loadData
which accepts an object as input and assigns all properties of that object to the class instance.
To ensure proper type checking, I have implemented an interface called GreetingsInterface for both the class and the object parameter.
interface GreetingsInterface {
message: string;
name: string;
lastName: string;
age: number;
}
class GreetingsManager implements GreetingsInterface {
constructor(message: string) {
this.message = message;
}
loadData(obj: object) {
Object.assign(this, obj)
return this;
}
greet() {
return "Hello, " + this.message;
}
}
let data: GreetingsInterface = {
message: 'Hi',
lastName: 'Smith',
name: 'John',
age: 25
}
let greetings = new GreetingsManager('Bonjour').loadData(data);
let button = document.createElement('button');
button.textContent = "Display Greetings";
button.onclick = function() {
console.log(greetings)
}
document.body.appendChild(button);
However, upon compilation, an error is thrown stating that GreetingsManager does not contain a property named 'message'.