1

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!

JiwanJeon94
  • 385
  • 4
  • 12
  • You dont need the interface. Maybe this can help: https://stackoverflow.com/questions/2866987/what-is-the-definition-of-interface-in-object-oriented-programming – Luca Kiebel Jan 17 '22 at 11:37
  • @LucaKiebel That's not what OP is asking. – Fildor Jan 17 '22 at 11:38
  • In short - interface just describes what variables-methods you can expect [from object, implementing said interface], it doesn't create any variables or methods itself. In TS, interface declaration doesn't generate any code in compiled JS. – Arvo Jan 17 '22 at 13:44
  • @Arvo First of all, thank you for your comment! Then, the interface is for only developers not for computers(like a compiler)? – JiwanJeon94 Jan 17 '22 at 15:14
  • Compiler uses that information for type checking and to ensure your classes implement interface properly. IMO entire goal of typescript is to create good OOP environment for not so object oriented javascript. – Arvo Jan 18 '22 at 08:14
  • @Arvo Oh... I see I see I got your point! So, it is pretty standard? general? way to use both of them at the same time. Am I correct..? – JiwanJeon94 Jan 18 '22 at 12:01
  • They usually are not used at the same time or at least not in similar role :) Interface often defines some data structure, which is never used for implementation; another case is to declare some identical subset of properties and methods for very different kind of classes/objects. Your example is not the best one - your interface and class mostly overlap. For example - if you created some ColorInterface (with color property and paint method), then you could use it for getting current color or painting of any colored and/or paintable objects. Sorry, I'm not a teacher :( :) – Arvo Jan 18 '22 at 13:47
  • @JiwanJeon94 I have the same exact question! Have you found an explanation for this?! – Nojan A. Jul 23 '23 at 20:12

0 Answers0