简体   繁体   English

TypeScript型'承诺<void | object> ' 不可分配给类型 'Promise<object> ' 在 Promise.then()<div id="text_translate"><p> 我目前正在尝试以 base64 格式实现缓存文档的服务。 我希望这个服务从 sessionStorage 中获取文档,如果 sessionStorage 中没有文档,则从 IRequestService 中获取它,然后将其保存到 sessionStorage。 我尝试了这种方法,但出现类型错误</p><pre>Type 'Promise&lt;void | GetDocumentResult&gt;' is not assignable to type 'Promise&lt;GetDocumentResult&gt;'.</pre><p> 代码如下所示:</p><pre> getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise&lt;GetDocumentResult&gt; { if (this.storageKey in sessionStorage) { const documentsMap: Map&lt;DocumentID, GetDocumentResult&gt; = new Map(JSON.parse(sessionStorage.getItem(this.storageKey).)) if (documentsMap.get(documentId).== undefined) { return new Promise(resolve =&gt; resolve(documentsMap,get(documentId).)) } } return requestService.getDocument(requestId, documentId) .then(value =&gt; {this.setDocument(documentId, value)}) }</pre><p> 我期待 Promise&lt;GetDocumentResult&gt;.then 的返回类型是 Promise&lt;GetDocumentResult&gt;,但由于某种我不明白的原因,它是 Promise&lt;void | 获取文档结果&gt;</p><p> 有人知道为什么会这样吗?</p><p> 谢谢</p></div></object></void>

[英]TypeScript Type 'Promise<void | Object>' is not assignable to type 'Promise<Object>' on Promise.then()

I am currently trying to implement a service for caching document in base64 format.我目前正在尝试以 base64 格式实现缓存文档的服务。 I want this service to fetch a document from sessionStorage, and if there is no document in sessionStorage, fetch it from IRequestService and then save it to sessionStorage.我希望这个服务从 sessionStorage 中获取文档,如果 sessionStorage 中没有文档,则从 IRequestService 中获取它,然后将其保存到 sessionStorage。 I tried this approach, but I am getting a type error我尝试了这种方法,但出现类型错误

Type 'Promise<void | GetDocumentResult>' is not assignable to type 'Promise<GetDocumentResult>'.

The code looks like this:代码如下所示:

getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise<GetDocumentResult> {
    if (this.storageKey in sessionStorage) {
      const documentsMap: Map<DocumentID, GetDocumentResult> = new Map(JSON.parse(sessionStorage.getItem(this.storageKey)!))
      if (documentsMap.get(documentId) !== undefined) {
        return new Promise(resolve => resolve(documentsMap.get(documentId)!))
      }
    }

    return requestService.getDocument(requestId, documentId)
      .then(value => {this.setDocument(documentId, value)})
}

I was expecting return type of Promise< GetDocumentResult >.then to be Promise< GetDocumentResult >, but for some reason which I dont understand, it's Promise<void |我期待 Promise<GetDocumentResult>.then 的返回类型是 Promise<GetDocumentResult>,但由于某种我不明白的原因,它是 Promise<void | GetDocumentResult>获取文档结果>

Do someone knows why this is happening?有人知道为什么会这样吗?

Thank you谢谢

Most likely you have an issue because you are not returning anything inside your .then()很可能您遇到了问题,因为您没有在.then()中返回任何内容

here:这里:

 .then(value => {this.setDocument(documentId, value)})

and that makes your returned type be Promise<void> instead of Promise<GetDocumentResult>这使您返回的类型是Promise<void>而不是Promise<GetDocumentResult>

final versions should be like here:最终版本应该像这里:

getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise<GetDocumentResult> {
    if (this.storageKey in sessionStorage) {
      const documentsMap: Map<DocumentID, GetDocumentResult> = new Map(JSON.parse(sessionStorage.getItem(this.storageKey)!))
      if (documentsMap.get(documentId) !== undefined) {
        return new Promise(resolve => resolve(documentsMap.get(documentId)!))
      }
    }

    return requestService.getDocument(requestId, documentId)
      .then(value => { return this.setDocument(documentId, value)})
}

As already explained, your then callback does return void not the result.如前所述,您的then回调确实返回void而不是结果。 You're looking for您正在寻找

getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise<GetDocumentResult> {
  if (this.storageKey in sessionStorage) {
    const documentsMap: Map<DocumentID, GetDocumentResult> = new Map(JSON.parse(sessionStorage.getItem(this.storageKey)!));
    const document = documentsMap.get(documentId);
    if (document !== undefined) {
      return Promise.resolve(document);
    }
  }

  return requestService.getDocument(requestId, documentId).then(value => {
    this.setDocument(documentId, value);
    return value;
//  ^^^^^^^^^^^^
  });
}

Btw, I would recommend to extract the cache lookup and storage ( setDocument ?) into a separate service, and to have that service keep the documentsMap in memory instead of parsing JSON every time getDocument is called.顺便说一句,我建议将缓存查找和存储( setDocument ?)提取到单独的服务中,并让该服务将documentsMap保存在 memory 中,而不是每次getDocument时都解析 JSON 。

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

相关问题 打字稿:类型&#39;Promise &lt;{}&gt;&#39;不能分配给&#39;Promise&#39; <void> “ - Typescript : Type 'Promise<{}>' is not assignable to type 'Promise<void>' 打字稿:输入“承诺”<void> &#39; 不可分配给类型 &#39;void | 破坏者 - Typescript: Type 'Promise<void>' is not assignable to type 'void | Destructor' &#39;诺言 <void> &#39;不可分配给&#39;Promise <IListItem[]> - 'Promise<void>' is not assignable to type 'Promise<IListItem[]> 打字稿:类型 &#39;Promise&lt;{}&gt;&#39; 不可分配给类型 - Typescript: Type 'Promise<{}>' is not assignable to type 反应| Promise.then()上的Promise Uncaught类型错误 - React | Promise Uncaught Type Error on Promise.then() Promise 参数类型不可分配 - Promise argument type is not assignable typescript,新的Promise object是什么型号? - typescript, what is the type of a new Promise object? 打字稿抱怨诺言。然后 - Typescript complains on promise.then angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string 在“ Promise.then()”中返回带有“ then”功能的对象 - Return an object with a “then” function within “Promise.then()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM