简体   繁体   English

打字稿:类型&#39;Promise &lt;{}&gt;&#39;不能分配给&#39;Promise&#39; <void> “

[英]Typescript : Type 'Promise<{}>' is not assignable to type 'Promise<void>'

I have the following function : 我有以下功能:

let templateLoader = (onDidFinishLoad : Function, onDidFailLoad : Function) =>
    (url : string) : Promise<void> => 
        new Promise(    
            (resolve,reject) => {
                mainWindow.loadURL(url);
                mainWindow.webContents.once(
                    'did-finish-load',
                    () => { 
                        onDidFinishLoad(resolve);
                    }  
                );  
                mainWindow.webContents.once(
                    'did-fail-load', 
                    (event,errorCode,errorDescription) => {  
                        onDidFailLoad(reject,errorDescription); 
                    }  
                );   
            } 
        ); 

I've got the following compilation error: 我有以下编译错误:

ERROR in [at-loader] ./app/loaders.ts:9:9 TS2322: Type 'Promise {}' is not assignable to type Promise void. [at-loader]中的错误./app/loaders.ts:9:9 TS2322:类型'Promise {}'不能分配给Promise void类型。 Type '{}' is not assignable to type 'void'. 类型“{}”不能指定为“void”类型。

it works by modifying : Promise<void> into : Promise<any> , 它的工作原理是修改: Promise<void> into : Promise<any>

or to cast new Promise into new Promise<void> . 或者将new Promise投入new Promise<void>

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.

相关问题 打字稿:输入“承诺”<void> &#39; 不可分配给类型 &#39;void | 破坏者 - Typescript: Type 'Promise<void>' is not assignable to type 'void | Destructor' 打字稿:类型 &#39;Promise&lt;{}&gt;&#39; 不可分配给类型 - Typescript: Type 'Promise<{}>' is not assignable to type &#39;诺言 <void> &#39;不可分配给&#39;Promise <IListItem[]> - 'Promise<void>' is not assignable to type 'Promise<IListItem[]> 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 参数类型不可分配 - Promise argument type is not assignable angular2 TS2322类型&#39;Promise <void> &#39;不可分配给&#39;Promise <string> &#39;当给定字符串时 - angular2 TS2322 Type 'Promise<void>' is not assignable to type 'Promise<string>' when given a string 反应 TS - 类型 '() =&gt; Promise<void> ' 不可分配给类型 'EventHandler<clickevent, boolean | void> '</clickevent,></void> - React TS - Type '() => Promise<void>' is not assignable to type 'EventHandler<ClickEvent, boolean | void>' RxDB Angular2-cli&#39;承诺 <void> &#39;不能赋值给&#39;Promise&#39;类型的参数 <any> “ - RxDB Angular2-cli 'Promise<void>' is not assignable to parameter of type 'Promise<any>' Typescript 类型 boolean 不可分配给 void - Typescript type boolean is not assignable to void Typescript Promise 拒收型 - Typescript Promise rejection type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM