Created
July 2, 2017 11:31
-
-
Save Armenvardanyan95/9b19282d64ee24a9073e6cff0f5be845 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Movie { | |
| id: number; | |
| title: string; | |
| } | |
| interface User { | |
| firstName: string; | |
| lastName: string; | |
| age: number; | |
| favoriteMovies: Array<Movie | number>; | |
| /* | |
| notice how we supposed that this property | |
| may be either an Array of Movie objects | |
| or of numerical identificators | |
| */ | |
| } | |
| class UserModel implements User { | |
| firstName: string; | |
| lastName: string; | |
| age: number; | |
| favoriteMovies: Array<Movie | number>; | |
| constructor(source: User){ | |
| this.firstName = source.firstName; | |
| this.lastName = source.lastName; | |
| this.age = source.age; | |
| this.favoriteMovies = source.favoriteMovies.map((movie: Movie) => movie.id); | |
| /* | |
| we moved the data manipulation to this separate class, | |
| which is also a valid representation of a User model, | |
| so no unnecessary clutter here | |
| */ | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment