Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object creation using class vs interface in Typescript (Angular 5 ) vs Java and C#

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.

like image 443
phonemyatt Avatar asked Nov 28 '17 16:11

phonemyatt


People also ask

Should I use interface or class in TypeScript?

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.

What is the difference between interface and object in TypeScript?

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).

Is TypeScript interface same as Java?

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.

What is the difference between class and object in TypeScript?

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.


1 Answers

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.

like image 57
Crappy Avatar answered Sep 20 '22 11:09

Crappy