2016-06-23 16 views
4

Wie werde ich Typoskript-Fehler für die Datenstruktur los, die in eine then Versprechung übergeben wurde?Eigenschaft 'count' existiert nicht für den Typ '{'} '

Ich erhalte die folgende Fehlermeldung:

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

Für den folgenden 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; 
    } 
    } 
}); 

Ich vermute, ich habe socialLinks irgendwie zu definieren, aber wo nicht funktioniert.

Antwort

5

Der üblicher Weg ist eine Art Schnittstelle zu erstellen und sie als eine Art verwenden:

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

Im Fall gibt es mehr klar ist, welche T ist, z.B. IPerson ... können wir IData<IPerson>

Wiedergabe mit that here

+1

funktioniert perfekt dank verwenden. –

Verwandte Themen