简体   繁体   English

打字稿; 承诺返回类型不匹配

[英]typescript; promise return type mismatch

I have a method that compiled fine when i was running the beta of angular 2. now that i upgraded to v4, I'm getting an error I can't seem to solve. 当我运行角度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);
}

I get Type 'Promise<Response>' is not assignable to type 'Promise<String>'. 我得到Type 'Promise<Response>' is not assignable to type 'Promise<String>'.

As far as I can tell my method returns a string at the end of the promise chain (return Promise.resolve(fileURL)) 据我所知,我的方法在promise链的末尾返回一个字符串(返回Promise.resolve(fileURL))

I have a guess and it is a hack as there are probably better solutions, but you could attempt to bypass the compiler error by casting the resolved promise to any... 我有一个猜测,它是一个黑客,因为可能有更好的解决方案,但你可以尝试通过将解决的承诺强制转换为任何...来绕过编译器错误...

Try this: 尝试这个:

return this.http.put(uploadURL, file)
          .toPromise()
          .then(response =>{
            return Promise.resolve(fileURL) as any;
          });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM