打字稿:智能感知不承认使用方法返回正确的类型http.post
我有以下的代码打字稿:智能感知不承认使用方法返回正确的类型http.post
getLoggingCsvRecords(recId: string) {
const url = this.apiurl + 'loggingcsvrecordsforrecid';
return this.http.post<Array<string>>(url, {recId})
.map(data => data['results']);
}
我希望智能感知认识到,由getLoggingCsvRecords()
方法返回类型为Obsevable<Array<string>>
。 相反,智能感知暗示Observable<any>
是正确的类型。 我错在哪里?
我正在使用VSCode作为IDE。
您在错误的地方返回签名。
GetLoggingRecords(recId: string): Observable<Array<string>> {
Your code
return this.http.post(rest of stuff)
}
根据你写的内容,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
。对属性的字符串访问可能会混淆语法分析器。
不行的,因为这是仍然可观察
强权铸造值得检查官方文件,因为很难说确切的回报类型。 https://angular.io/guide/http#typechecking-the-response但总结“然而,你可以告诉HttpClient响应是什么类型,这是建议的”,如上所述。 –
我读的地方可能写出'http.post()'但可能我不明白真正意义上的 –
Picci
这可能是您要避免 –