簡體   English   中英

TypeScript在Promise.then中編譯錯誤

[英]Typescript compile error in promise.then

我正在嘗試在打字稿中使用Promise。 我使用tsd安裝es6-promise,並將引用添加到我的文件中。 它在大多數情況下都了解諾言,但是我很難將它們與then

function foo() : Promise<any> {
  var p: Promise<any> = Promise.resolve();
  var x: Promise<any> = Promise.resolve();
  p = p.then(x);
  return p;
}

給我這個在p.then(x)的位置:

錯誤TS2345:類型'Promise'的參數不能分配給類型'(value:any)=> {} | thenable <{}>'。

這是類型定義中的錯誤,還是我使用的錯誤?

p.then(x);

這是無效的。 x需要起作用。 在這里,您可以兌現承諾。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 打字稿抱怨諾言。然后 反應| Promise.then()上的Promise Uncaught類型錯誤 Promise.then 不是僅在服務器上的函數錯誤 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> 承諾然后綁定問題 JavaScript Promise.then回滾 承諾,然后不被調用 TypeError:promise.then不是函數 使用 Jasmine 2 測試 Promise.then Promise.then不在Jasmine中執行
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM