繁体   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