简体   繁体   中英

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.

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>'.

As far as I can tell my method returns a string at the end of the promise chain (return 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;
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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