I have used Interface to create Data Transfer Objects (DTO), "dumb" objects that only serve to standardize the communication of objects between the front and the back. My question is: Really using Interface is best practice? Or would it be better to use classes? What do you think about it?
Ex:
interface UserDTO {
name?: string;
mailAddress?: string;
phone?: string;
}
params: UserDTO;
_save(user): void {
this.params = {};
this.params.name = user.name;
this.params.mailAddress = user.mailAddress;
this.params.phone = user.phone;
[...]
}
I just have to set the properties to nullable
to be able to initialize the interface with empty object, so that null reference exception does not occur. That's exactly what's bothering me. If I use class, I would solve this by instantiating a User
object. I hope I have been clear.