user3745073 user3745073 - 22 days ago 7
TypeScript Question

Angular 2: Why do I need classes like Hero.ts?

I am a web developer coming from Angular 1.3. Now I'm learning Angular 2 and there is one thing I can't get clear:

When I have a page where I can view heroes, I would have a controller and a service to get the data e.g. from a REST-API in Angular1.

In Angular2 I have the component, a service and a Hero class. Why do I need that class 'Hero'?

What are the advantages in comparison to just using objects like in Angular1? Is it best practice to implement a class for every object I use in my application?

Thanks a lot!

Answer

If it's the Hero class from https://angular.io/docs/ts/latest/tutorial/toh-pt1.html then it's just the data the component is working with (display, edit, ...)

As mentioned in the comments. If concrete classes are used you get full benefit of the static type checking capabilities of TypeScript and related tools.