After watching a tutorial from my teacher, he showed us this code snippet:
https://i.sstatic.net/MA3Z9.png
He mentioned that the products
array, defined as type any []
, is not taking advantage of TypeScript's strongly typing. He suggested using an INTERFACE instead. I have a question though, why didn't he create a class and define the products array with that class type?
According to him, Interfaces are meant to provide strong typing and tooling support.. Hence, he created this interface:
https://i.sstatic.net/BMO69.png
I'm puzzled as to why he opted for an Interface as the data type here instead of a class like:
export class Product
.. and then use products : Product []
..
So, I can't wrap my head around why Interface is considered better here than a class for data type?
Apologies for the screenshots instead of actual code, as his video lessons are hosted online.
Thanks