Bill Noble
Bill Noble

Reputation: 6744

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

How do I get rid of Typescript errors for the data structure passed into a then promise?

I am getting the following error:

Property 'count' does not exist on 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 am guessing I have to define socialLinks somehow but can't work out where.

Upvotes: 4

Views: 4947

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

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, there is more clear what T is, e.g. IPerson... we can use IData<IPerson>

Play with that here

Upvotes: 5

Related Questions