簡體   English   中英

打字稿抱怨諾言。然后

[英]Typescript complains on promise.then

考慮以下代碼:

Promise.resolve('hello')
    .then(a => console.log(a))
    .catch(() => console.log("error"))
    .then(() => console.log("ok"));

這讓我有一個非常煩人的錯誤:

提供的參數與調用目標的任何簽名都不匹配:預期的0個參數但是使用1個參數調用

請注意,我只在第一個.then(...)得到這個,然后捕獲和第二個顯然沒有問題。

僅供記錄; 代碼工作和輸出

hello
ok

也許我還應該提一下它實際編譯。 我看到在Visual Studio 2017年的錯誤,當我去的,然后定義它打開lib.es6.d.ts。 使用TypeScript 2.8.3的webpack 4進行編譯。 在Visual Sudio中我安裝了TypeScript 2.8.1(找不到2.8.3)

如果有人能向我解釋為什么會這樣。 甚至更好,如何擺脫它; 非常感謝。

我有同樣的問題,正如@JBC的評論暗示的那樣,它是由ReSharper引起的。 在我的情況下,我運行了一個相當舊的版本(2017.1.1),所以更新到最新(或完全禁用它)修復它。

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.

相關問題 TypeScript在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> Promise.then不執行 Promise.then是否異步? 承諾然后綁定問題 JavaScript Promise.then回滾 承諾,然后不被調用 TypeError:promise.then不是函數 使用 Jasmine 2 測試 Promise.then Promise.then不在Jasmine中執行
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM