简体   繁体   English

打字稿抱怨诺言。然后

[英]Typescript complains on promise.then

Consider following code: 考虑以下代码:

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

This gets me a very annoying error: 这让我有一个非常烦人的错误:

supplied parameters do not match any signature of call target: expected 0 parameter(s) but was called with 1 parameter(s) 提供的参数与调用目标的任何签名都不匹配:预期的0个参数但是使用1个参数调用

Note that I only get this for the first .then(...) , the catch and the second then are apparently no problem. 请注意,我只在第一个.then(...)得到这个,然后捕获和第二个显然没有问题。

Just for the record; 仅供记录; the code works and outputs 代码工作和输出

hello
ok

Maybe I should also mention that it actually compiles. 也许我还应该提一下它实际编译。 I see the error in Visual Studio 2017. When I go to the definition of then it opens lib.es6.d.ts . 我看到在Visual Studio 2017年的错误,当我去的,然后定义它打开lib.es6.d.ts。 Compilation happens with webpack 4 that uses TypeScript 2.8.3. 使用TypeScript 2.8.3的webpack 4进行编译。 In Visual Sudio I have installed TypeScript 2.8.1 (Can't find 2.8.3) 在Visual Sudio中我安装了TypeScript 2.8.1(找不到2.8.3)

If someone could explain to me why this happens. 如果有人能向我解释为什么会这样。 And even better, how to get rid of it; 甚至更好,如何摆脱它; that would be much appreciated. 非常感谢。

I had the same issue and as the comment by @JBC alluded, it's caused by ReSharper. 我有同样的问题,正如@JBC的评论暗示的那样,它是由ReSharper引起的。 In my case I was running a rather old version (2017.1.1) so updating to the latest (or disabling it altogether) fixed it. 在我的情况下,我运行了一个相当旧的版本(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 compile error in 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> - TypeScript Type 'Promise<void | Object>' is not assignable to type 'Promise<Object>' on Promise.then() Promise.then不执行 - Promise.then not executing Promise.then是否异步? - Is promise.then asynchronous or not? 承诺然后绑定问题 - Promise.then bind issue JavaScript Promise.then回滚 - JavaScript Promise.then rollback 承诺,然后不被调用 - Promise.then not being invoked TypeError:promise.then不是函数 - TypeError: promise.then is not a function 使用 Jasmine 2 测试 Promise.then - Testing Promise.then with Jasmine 2 Promise.then不在Jasmine中执行 - Promise.then is not executing in Jasmine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM