In the company in which I work, it is customary to model entities using Interfaces when developing with Typescript.

To me, it seems much more effective to model entities with classes and then create instances wherever an object of that type is needed.

When I talked about it with my colleagues including the CTO, I asked what is the best way to create new object characterized by some interface, and was told to create a function that returns an object containing all the fields matching to that interface initialized to null or undefined.

To me, this just looks like a constructor.

I would love to hear an explanation of why using that technique is better than generating classes and instances, or what are the best practices for that scenario.

Also, are typescript interfaces not standard OOP interfaces? is the name misleading me?

  • To me, this just looks like a constructor. -- Technically, it is a Factory Method. – Robert Harvey Jul 10 at 15:37
  • @RobertHarvey Then why use interfaces+factory methods over classes+constructors? – Gulzar Jul 14 at 7:19
  • Did you read the article I linked? It explains why. – Robert Harvey Jul 14 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.