Search code examples
angulartypescriptweblogic

What is the Logic behind the User Class? Angular 5


I am in Angular 5. This is a conceptual question, so let me explain it with an example:

I have a list of users that is only available for logged users. The user list just needs the name and the user ID. And for the user logged, I need tokens for authentications.

So my question is: Do I have to create 2 users classes?, one for the user from the login, with all the tokens, and another one without the tokens, and just with the name and the id fields.

Or maybe, can I create just one User class, with the fields for the tokens and that user class, use it for the user list?.

Which one is the best practice?

Thanks


Solution

  • Better to have one User class with optional property.

    class User {
      id: number;
      name: string;
      token?: string;
    }
    
    user1 = { id: 1, name:"User 1", token:"token1"};
    user2 = { id: 1, name:"User 1"};