I am just getting started learning TS these days. I have a question about class and Interface.
According to below code,
// Classes with Interfaces
interface PersonInterface {
id: number;
name: string;
register(): string;
}
class Person1 implements PersonInterface {
id: number;
name: string;
constructor(id: number, name: string) {
// console.log(123); this could be console whenever the object run
this.id = id;
this.name = name;
}
register() {
return `${this.name} is now registered`;
}
}
class Employee extends Person1 {
position: string;
constructor(id: number, name: string, position: string) {
super(id, name);
this.position = position;
}
}
const emp = new Employee(3, "Jiwan", "Developer");
console.log(emp.name);
console.log(emp.register());
I am not sure Why do we need to declare the same value and type twice in classes with Interface. If I have to declare here and there without any reason, I understand how does TS works. However, I am wondering if there is a specific reason here.
In advance, I am so sorry If I ask a stupid question. I am a pretty beginner. Thank you for your help!