Greg Gum
Greg Gum

Reputation: 38099

How to type JSON results

I would like to convert this js to ts:

class foo{

   getSection() {
       return dataService.getSection('HOME_PAGE').then(data => {
          this.section(data.results[0]); //Error Here
    });
    }
}

The compile time error message is: Property 'results' does not exist on type 'void'.

data is a JSON result set, and results[0] is simple the first record.

How do I type this properly to resolve this error?

Upvotes: 0

Views: 58

Answers (1)

basarat
basarat

Reputation: 276235

The issue is that dataService.getSection('HOME_PAGE') is being inferred to return Promise<void> and therefore data is of type void.

Fix in your dataService:

getSection(secName: string): Promise<any>

Or something stronger if you don't want any.

(Some basic docs on annotations : http://basarat.gitbooks.io/typescript/content/docs/types/type-system.html)

Upvotes: 1

Related Questions