Property 'count' does not exist in type '{}'

How to get rid of Typescript errors for a data structure passed in a promise then?

I get the following error:

Property 'count' does not exist in type '{}'

For the following code:

this.userData.getSocialLinks(this.navParams.get('image_owner_id')).then(socialLinks => {
  var i = 0;
  for (i=0; i < socialLinks.count; i++) {
    if (socialLinks.results[i].social_type == 'TW') {
      this.profile.twitter = socialLinks.results[i].social_address;
      this.isExistingTW = true;
      this.twEntryID = socialLinks.results[i].id;
    }
    else if (socialLinks.results[i].social_type == 'IN') {
      this.profile.instagram = socialLinks.results[i].social_address;
      this.isExistingIN = true;
      this.inEntryID = socialLinks.results[i].id;
    }
  }
});

I assume that I need to somehow determine socialLinks, but I can not decide where.

+4
source share
1 answer

The standard way is to create some kind of interface and use it as a type:

// describe what is coming    
export interface IData<T> {
    count: number;    
    results: T[];
}

// use that IData
this.userData
 .getSocialLinks(this.navParams.get('image_owner_id'))
 .then(( socialLinks: IData<any>) => {

In case it is more clear what T is, i.e. IPerson... we can useIData<IPerson>

Play with here

+5
source

All Articles