I have an Object where I use it in multiple services, and each one should take some parameters, so I create two constructors, but TypeScript did not allow me to do this. My example is:
class User {
id: number;
username: string;
password: string;
email: string;
firstName: string;
lastName: string;
roles: string[];
constructor(username: string, password: string){
this.username = username;
this.password = password;
}
constructor(id: number, username: string, firstname: string, lastname: string, roles: string[]){
this.id = id;
this.username= username;
this.firstname= firstname;
this.lastname= lastname;
this.roles = roles;
}
//.. and maybe another constructor also
}
Is there a trick to solve this issue, please?
When I use the optional ? in constructors for example:
constructor(
public id?: number,
public username?: string,
public email?: string,
public password?: string,
public firstName?: string,
public lastName?: string,
public roles?: string[]) {
}
and when I get my data from backend:
this.service.usersList().subscribe(users => {
console.log(users);
this.dataSource.data = users;
});
The roles is set in the password and not in the roles failed:
{
"id": 1,
"username": "user1",
"email": "[email protected]",
"password": [
"USER",
"MODR"
]
}
For that I'm not sure about this trick.
Maybe I was not precise, but I use this method to parse my data:
static fromJson(item: Object): any {
return new User(
item['id'],
item['username'],
item['email'],
item['roles']
);
}
For that, when I create a constructor with optional, it will set the attributes in order of my call.
?optional parameter. Which fields will be mandatory always??for optional, it fail in some casesoptional parameters, and verify if the needed parameters exists to implement one of the two constructors , see my answer below