2016-06-23 66 views
4

如何摆脱传递到then承诺的数据结构的Typescript错误?类型'{}'上不存在属性'count'

我收到以下错误:

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

对于下面的代码:

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; 
    } 
    } 
}); 

我猜我必须定义socialLinks莫名其妙,但不能在哪里工作。

回答

5

的标准方法,是创造某种接口,并把它作为一个类型:

// 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>) => { 

在情况下,有更清楚T是,例如IPerson ......我们可以用IData<IPerson>

播放与that here

+1

作品完美的感谢。 –

相关问题