Typescript:intellisense无法识别使用http.post的方法返回的正确类型

时间:2017-10-13 12:07:51

标签: angular typescript intellisense angular-http

我有以下代码

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

我希望intellisense能够识别getLoggingCsvRecords()方法返回的类型是Obsevable<Array<string>>。 相反,intellisense建议Observable<any>为正确的类型。 我哪里错了?

我使用VSCode作为IDE。

2 个答案:

答案 0 :(得分:2)

你的签名在错误的地方。

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

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

答案 1 :(得分:-1)

根据您编写的内容,HTTP Post返回一个字符串数组。但是,您似乎将其映射为对象。

也许你的意思是这样的?

interface ILoggingRecord {
    results: string;
}

// ...

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

调用应该能够理解数据是一个对象数组。因此,地图的结果是一个字符串。如果这不起作用,请尝试使用data => data.results。对属性的字符串访问可能会混淆语法分析器。