【发布时间】:2021-05-13 04:10:24
【问题描述】:
在 Electron-React-Typescript 应用程序中,我收到此错误:Property 'api' does not exist on type 'Window & typeof globalThis'. window.api.send('open-type-A-window', '');
但是在 index.d.ts 文件中,我以这种方式声明了接口 Window:
declare global {
namespace NodeJS {
declare interface Window {
"electron": {
openNewWindow(): void;
},
"api": {
send: (channel, data) => {
ipcRenderer.invoke(channel, data).catch(e => console.log(e))
},
receive: (channel, func) => {
console.log("preload-receive called. args: ");
ipcRenderer.on(channel, (event, ...args) => func(...args));
},
electronIpcSendTo: (window_id: string, channel: string, ...arg: any) => {
ipcRenderer.sendTo(window_id, channel, arg);
},
electronIpcSend: (channel: string, ...arg: any) => {
ipcRenderer.send(channel, arg);
},
electronIpcSendSync: (channel: string, ...arg: any) => {
return ipcRenderer.sendSync(channel, arg);
},
electronIpcOn: (channel: string, listener: (event: any, ...arg: any) => void) => {
ipcRenderer.on(channel, listener);
},
electronIpcOnce: (channel: string, listener: (event: any, ...arg: any) => void) =>
{
ipcRenderer.once(channel, listener);
},
electronIpcRemoveListener: (channel: string, listener: (event: any, ...arg: any)
=> void) => {
ipcRenderer.removeListener(channel, listener);
},
electronIpcRemoveAllListeners: (channel: string) => {
ipcRenderer.removeAllListeners(channel);
}
}
}
}
}
我已经阅读了这个帖子:https://github.com/Microsoft/TypeScript/issues/19816,但我没有得到正确的解决方案。
为了避免这个错误Property 'api' does not exist on type 'Window & typeof globalThis',我应该添加/修改什么?
- 节点:v14.5.0
- 电子:v11.2.3
- 打字稿:v4.1.3
- 操作系统:Ubuntu 18.04.4 桌面
【问题讨论】:
-
"api", {好像打错了 -
@p4m 谢谢。不幸的是,即使在纠正了这个错字之后,我也得到了
Property 'api' does not exist on type 'Window & typeof globalThis'
标签: reactjs typescript electron