Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created July 2, 2017 11:31
Show Gist options
  • Save Armenvardanyan95/9b19282d64ee24a9073e6cff0f5be845 to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/9b19282d64ee24a9073e6cff0f5be845 to your computer and use it in GitHub Desktop.
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