简体   繁体   English

TypeScript在Promise.then中编译错误

[英]Typescript compile error in promise.then

I'm trying to use promises in typescript. 我正在尝试在打字稿中使用Promise。 I used tsd to install es6-promise and added the reference to my file. 我使用tsd安装es6-promise,并将引用添加到我的文件中。 It understands promises for the most part, but I'm having difficulty chaining them with then . 它在大多数情况下都了解诺言,但是我很难将它们与then

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

gives me this at the location of p.then(x) : 给我这个在p.then(x)的位置:

error TS2345: Argument of type 'Promise' is not assignable to parameter of type '(value: any) => {} | 错误TS2345:类型'Promise'的参数不能分配给类型'(value:any)=> {} | Thenable<{}>'. thenable <{}>'。

Is this somehow a bug in the type definition , or am I using it wrong? 这是类型定义中的错误,还是我使用的错误?

p.then(x); p.then(x);

This is invalid. 这是无效的。 x needs to be function. x需要起作用。 Here you have it as a promise. 在这里,您可以兑现承诺。

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 complains on promise.then 反应| Promise.then()上的Promise Uncaught类型错误 - React | Promise Uncaught Type Error on Promise.then() Promise.then 不是仅在服务器上的函数错误 - Promise.then is not a function error only on server 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 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