Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 models: interface vs classes

I have a login/registration module and I want to write a model for them.

I would use interfaces, but I need to have one predefined value.

And I was wondering - should I put the predefined out as a constant (it won't be changed, altered) and use interfaces. Or write it as classes (as currently)

Currently, I wrote two separate classes - registration.model.ts, login.model.ts, could I be able to abstract to use only one model? (example: user.model.ts)

Some examples:

export class LoginUser {
  constructor(
    private email,
    private password,

    // I need to have it, backend expects it to be sent
    private connection = 'Username-Password-Authentication'  
  ) { }
} 


export class RegistrateUser {
  constructor(
    public name: string,
    public lastName: string,
    public email: string,
    public password: string,

    // I need to have it, backend expects it to be sent
    private connection = 'Username-Password-Authentication'
  ) { }
}
like image 711
Tomas Eglinskas Avatar asked May 05 '17 15:05

Tomas Eglinskas


People also ask

Should I use class or interface in angular?

Interfaces are great when you only need the type checking whereas classes are great when you not only want the type checking, but you need some methods or require some other logic. Personally, I always start with just using an interface, and once I need some methods, I'll add a class and inherit the interface.

What is difference between model class and interface in angular?

A class is a blueprint from which we can create objects that share the same configuration - properties and methods. An interface is a group of related properties and methods that describe an object, but neither provides implementation nor initialisation for them.

Should I use classes or interfaces 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.

Why we are using interface in angular?

An interface is a way to define a contract on a function with respect to the arguments and their type. Along with functions, an interface can also be used with a Class as well to define custom types. An interface is an abstract type, it does not contain any code as a class does.


2 Answers

just came across your question & was investigating the same thing.

With reference to the Angular 2 Style docs here, the 'guideline' is to NOT implement an interface on the premise that a class alone can:

  • Act as an interface (if you use the implements keyword instead of the extends keyword)
  • Be smaller than a class-implementing-interface

As a rule of thumb:

Angular Guidelines > Typescript Guidelines (in the case of Angular 2 projects).

Hope this helps you out! :)

like image 185
jarodsmk Avatar answered Nov 09 '22 09:11

jarodsmk


I would definitely use interfaces. It is a simple approach and follows the TypeScript recommendations for working with JSON.

The property in question,

private connection = 'Username-Password-Authentication';

can well be added by the service that performs the request.

This will also reduce code duplication because you can use a generic function or service to create this request object.

For example:

export default function withRequiredAuthProps<T>(model: T) {
  return {...model, connection: 'Username-Password-Authentication'};
}

Then your Http service that sends the model back to the service can use a type constraint to verify that the property has been added before making the request

For example:

export default class MyHttpClient {
  post<T extends {connection: 'Username-Password-Authentication'}>(url: string, body: T) {
    //...
  }
}

These are just meant as examples but they are based on code that is minimal, and that works.

like image 43
Aluan Haddad Avatar answered Nov 09 '22 09:11

Aluan Haddad