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.