1

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?

Community
  • 1
  • 1
BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
  • Your interface defines `input` as `Object` in `deserialize`, but your implementation specifies it as `User`. Change your implementation to be `Object`, since you're not providing a `deserialize` function in your object (there's no such thing as a JSON object, just JSON as a string; once it's parsed, it's an object). – Heretic Monkey Dec 09 '16 at 20:29
  • @MikeMcCaughan I put ` lastSeenString: "" } as Object);` at the end of the mock user passed into the deserialize function. However I get this compile time error: `[ts] Argument of type 'Object' is not assignable to parameter of type 'User'. Property 'lastSeenString' is missing in type 'Object'.` – BeniaminoBaggins Dec 09 '16 at 20:38
  • 2
    No, change this line in your definition of `User`: `public deserialize(input: User) {` to be `public deserialize(input: Object) {`. Because you've defined it as `User`, it's looking for the `deserialize` method on that object you're passing in. – Heretic Monkey Dec 09 '16 at 20:47
  • @MikeMcCaughan Oh right. I had to change it to type `any` because it said type object doesn't have property this and that. But it's working now. Thank you! – BeniaminoBaggins Dec 09 '16 at 20:49

0 Answers0