Picci
Picci

Reputation: 17762

Typescript: intellisense does not recognize the right type returned by a method using http.post

I have the following piece of code

  getLoggingCsvRecords(recId: string) {
    const url = this.apiurl + 'loggingcsvrecordsforrecid';
    return this.http.post<Array<string>>(url, {recId})
                      .map(data => data['results']);
  }

I would expect intellisense to recognize that the type returned by the getLoggingCsvRecords() method is Obsevable<Array<string>>. On the contrary intellisense suggests Observable<any> as the right type. Where am I wrong?

I am using VSCode as IDE.

Upvotes: 0

Views: 283

Answers (2)

Eliott Robson
Eliott Robson

Reputation: 980

Based on what you have written the HTTP Post returns an array of strings. However, you appear to map this like an object.

Perhaps you meant something like this?

interface ILoggingRecord {
    results: string;
}

// ...

getLoggingCsvRecords(recId: string) {
    const url = this.apiurl + 'loggingcsvrecordsforrecid';
    return this.http.post<Array<ILoggingRecord>>(url, {recId})
        .map(data => data['results']);
}

The call should be able to understand that the data is an array of objects. The result of the map, therefore is a string. If this doesn't work try data => data.results instead. String access to properties can confuse the syntax analyser.

Upvotes: -1

Mike Tung
Mike Tung

Reputation: 4821

Your return signature in the wrong place.

GetLoggingRecords(recId: string): Observable<Array<string>> {
  Your code

  return this.http.post(rest of stuff)
}

Upvotes: 2

Related Questions