I want to understand using interface vs class to create model in angular 2. When we create model in Java or C#, we use model classes to represent data model that pass around in our application and therefore strongly typed.
//C#
public class Movie
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}
Most of the time we will stick to data binding with this kind of model to databases based on our requirements.
I see examples and tutorials using same approaches like this.
//Java
public class Movie
{
public int ID;
public String Title;
public Date ReleaseDate;
public String Genre;
public float Price;
}
Then, I start using angular and typescript for web development. I find that some like to use interface to represent their model and other like to stick with class.
//Class
export class Movie {
public string id;
public string title;
public Date ReleaseDate;
public string Genre;
public number
}
//Interface
export interface Movie {
public string id;
public string title;
public Date ReleaseDate;
public string Genre;
public number
}
However, when it comes to create and use model, it looks like same kind to me.
//declare
let movie = new Movie();
movie: Movie = new Movie();
movies: Movie[];
amovie: Movie;
//reactive
obsMovies: Observable<Movie[]>;
obsAMovie: Observable<Movie>;
I want to know the difference if has any when use interface or class in typescript. My understanding of interface is simply contract for classes that shared functions whereas class can be anything that can imagine.
When should we use classes and interfaces? If you want to create and pass a type-checked class object, you should use TypeScript classes. If you need to work without creating an object, an interface is best for you.
A class can be instantiated to create an object. An interface cannot be instantiated. The methods of a class are used to perform a specific action. The methods in an interface are purely abstract (the only declaration, not have a body).
It's two very different things. For starters Typescript relies on duck typing and Java doesn't. if you want to combine multiple interfaces it's better to use mapped types instead of extends a lot of times.
On a basic level, a class is an object that can be used to create other objects with a specific shape and functionality. It provides syntactic sugar for functionality that would be a lot more work to accomplish with plain objects and functions.
Classes in Typescript are more like your classes in Java and C#. They actually are getting compiled to JavaScript prototypes.
Interfaces are just a compile-time thing. They are just for the compiler to force static typing. You will not see anything from your Typescript-Interfaces in your compiled code.
Due to the fact that models often are just there to define a structure of data I am using the Typescipt-Interfaces for my models.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With