In order to group similar functionalities and have custom type checking, we are going to define classes for each entity used in our application. This will give us access to custom initialization and default values when creating entities.
Our first custom data type used in the frontend Angular application will be a user. You can use an interface to define a custom type or a regular class. If you need default values or custom validation, go with a regular class definition.
Create a new file called public/src/datatypes/user.ts
and add the following class:
export class User { _id: string; email: string; name: string; avatar: string; createdAt: string; constructor(_id?: string, email?: string, name?: string, createdAt?: string) { this._id = _id; this.email = email; this.name = name; this.avatar = 'http://www.gravatar.com/avatar/{{hash}}?s=50&r=g&d=retro'.replace('{{hash}}', _id); this.createdAt = createdAt; } }
When instantiating...