[英]Return Promise in Angular with typescript as custom Data Type
[英]typescript; promise return type mismatch
當我運行角度2的beta時,我有一個編譯好的方法。現在我升級到v4,我收到一個我似乎無法解決的錯誤。
uploadPDF(file):Promise<String>
{
return this.authHttp.get(this.presentationBaseUrl + "presignedUploadURL?file-name="+ encodeURIComponent(file.name)) <<<<< error here
.toPromise()
.then( data => {
var uploadURL = data.json().signedRequest,
fileURL = data.json().url;
return this.http.put(uploadURL, file)
.toPromise()
.then(response =>{
return Promise.resolve(fileURL)
});
})
.catch (this.handleError);
}
我得到Type 'Promise<Response>' is not assignable to type 'Promise<String>'.
據我所知,我的方法在promise鏈的末尾返回一個字符串(返回Promise.resolve(fileURL))
我有一個猜測,它是一個黑客,因為可能有更好的解決方案,但你可以嘗試通過將解決的承諾強制轉換為任何...來繞過編譯器錯誤...
嘗試這個:
return this.http.put(uploadURL, file)
.toPromise()
.then(response =>{
return Promise.resolve(fileURL) as any;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.