I want to create a mock User object from a standard JSON object.
I'm trying to implement #Option 4 on this SO answer.
However, when I create my mock user from the JSON object, I get this compile time error:
Property 'deserialize' is missing in type '{ id: number; userName: string; profilePhoto: string; coverPhoto: string; country: string; reputa...'.
deserialize
is a function, not a property, so I don't want to pass it in with my JSON object.
Here is my class:
interface iSerializable<T> {
deserialize(input: Object): T;
}
export class User implements iSerializable<User>{
public id?: number;
public userName?: string;
public country?: string;
public profilePhoto?: string;
public coverPhoto?: string;
public reputation?: number;
public description?: string;
public favourites?: Result[];
public followers?: User[];
public following?: User[];
public reviews?: Review[];
public catchPhrase?: string;
public itemsAdded?: string;
public topActivities?: string;
public recentActivity?: any[];
public lastSeen?: Date;
private _lastSeenString?: string;
get lastSeenString(): string {
return Time.timeSince(this.lastSeen);
} set lastSeenString(value: string) {
this._lastSeenString = value;
}
public badges?: string[];
public memberSince?: Date;
public deserialize(input: User) {
this.lastSeen = input.lastSeen;
this.badges = input.badges;
this.catchPhrase = input.catchPhrase;
this.country = input.country;
this.coverPhoto = input.coverPhoto;
this.description = input.description;
this.favourites = input.favourites;
this.followers = input.followers;
this.following = input.following;
this.id = input.id;
this.itemsAdded = input.itemsAdded;
this.memberSince = input.memberSince;
this.profilePhoto = input.profilePhoto;
this.recentActivity = input.recentActivity;
this.reputation = input.reputation;
this.reviews = input.reviews;
this.topActivities = input.topActivities;
this.userName = input.userName;
return this;
}
}
Here is my trying to create an instance of that class from JSON:
export var SERENA: User = new User().deserialize({
id: 1,
userName: "irishNimue",
profilePhoto: "profile-photo-images/serena.jpg",
coverPhoto: "cover-photo-images/ben-cover-photo.jpg",
country: "New Zealand",
reputation: 501,
description: "Interests include comics, wine, good food, surfing and exercising",
favourites: RESULTS,
followers: [BEN],
following: [BEN],
reviews: [
{
id: 11,
date: new Date("October 20, 2016 11:13:00"),
description: 'Suspendisse accumsan vehicula consequat. Nam ultrices egestas enim sit amet sollicitudin. Aenean volutpat enim nec sem dignissim, non consectetur mauris pharetra. Sed a maximus ante. Suspendisse potenti. Nunc ornare erat urna, egestas gravida nisi pretium vitae. Sed id leo nunc.',
rating: 2.1,
likes: 5,
user: SERENA,
comments: [{
id: 1,
description: "great review!",
user: BEN
}]
},
{
id: 12,
date: new Date("October 6, 2016 11:13:00"),
description: 'Suspendisse accumsan vehicula consequat. Nam ultrices egestas enim sit amet sollicitudin. Aenean volutpat enim nec sem dignissim, non consectetur mauris pharetra. Sed a maximus ante. Suspendisse potenti. Nunc ornare erat urna, egestas gravida nisi pretium vitae. Sed id leo nunc.',
rating: 6.9,
likes: 5,
user: SERENA,
comments: [{
id: 1,
description: "great review!",
user: BEN
}]
},
{
id: 13,
date: new Date("October 3, 2016 11:13:00"),
description: 'Maecenas dui dui, gravida et sagittis vitae, ornare ut urna. Proin id nulla ut ante lacinia tincidunt. Nullam eget risus blandit ex dapibus feugiat.',
rating: 7.1,
likes: 5,
user: SERENA,
comments: [{
id: 1,
description: "great review!",
user: BEN
}]
},
{
id: 14,
date: new Date("October 1, 2016 11:13:00"),
description: 'Vivamus fringilla leo eu cursus consequat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam a odio turpis.',
rating: 8.1,
likes: 5,
user: SERENA,
comments: [{
id: 1,
description: "great review!",
user: BEN
}]
}],
catchPhrase: "It's like a tiny doormat",
itemsAdded: null,
topActivities: null,
recentActivity: null,
lastSeen: new Date("October 13, 2016 11:13:00"),
badges: ["active User", "helper"],
memberSince: new Date("October 13, 2014"),
lastSeenString: ""
});
How do I get rid of the error and successfully create the instance of the User class from the JSON?