Doesn't work with multiple APIs that have different return values
Closed this issue · 2 comments
If I add multiple invoke APIs that have different return values then I get the following typescript error:
Type 'Promise<string | void>' is not assignable to type 'Promise<void>'.
Here is a reproducible example:
import './style.css';
import { GetApiType, ipcRenderer } from 'electron-typescript-ipc';
export type Api = GetApiType<
{
getDataFromStore: (str: string) => Promise<string>;
openUrlInBrowser: (str: string) => Promise<void>;
},
{
showAlert: (text: string, num: number) => Promise<void>;
}
>;
declare global {
interface Window {
myAPI: Api;
}
}
const api: Api = {
invoke: {
getDataFromStore: async (key: string) => {
return await ipcRenderer.invoke<Api>('getDataFromStore', key);
},
openUrlInBrowser: async (key: string) => {
return await ipcRenderer.invoke<Api>('openUrlInBrowser', key);
},
},
on: {
showAlert: (listener) => {
ipcRenderer.on<Api>('showAlert', listener);
},
},
};
And here is a codesandbox link (ignore the runtime error because obviously this isn't in an electron environment): https://codesandbox.io/s/practical-river-u5njd?file=/src/index.ts
This issue still persists, would you be able to look into it? @JichouP
@davej @pauliusuza
Thank you very much for your report, and I apologize for the long delay.
Thanks to the help of @sapphi-red , I think this Issue has been fixed at v3.0.0
!
Instead of exporting the ipcRenderer
function, the createIpcRenderer
function is exported. Please use this one.
And once you pass the type argument to createIpcRenderer
or createIpcMain
, you no longer need it afterwards!
See this example repo for more details
https://github.com/JichouP/electron-typescript-ipc-example