How to define a shape of object in typescript ? Should we do it with classes or interfaces?

  javascript, koa2, rest, typescript

I’m developing backend application in typescript. I want to define shape of an object. From typescript, I have options like classes, interfaces, types. Which one should I use?

Below are the docs recommending interfaces to define shapes.

https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html,

The below document says, its better to use interface on types

https://www.typescriptlang.org/play?e=83#example/types-vs-interfaces

they recommend using interfaces to define the shape of an object. But, we can’t instantiate an interface, and interfaces cannot hold data.

On the other hand, we have classes that define shape/blueprint of the object and can hold data.

So, should I go with classes to define shape and initialize it where ever I want?

where exactly should I user interfaces then, considering classes do all the work? What is the use-case of interfaces?

Please help me.

Source: Ask Javascript Questions

LEAVE A COMMENT