daltonmenezes / interprocess

💬 A scalable and type-safe Electron IPC management tool with enhanced DX
https://interprocess.daltonmenezes.com
MIT License
95 stars 2 forks source link

Typescript bug around empty parameters #6

Closed theogravity closed 1 year ago

theogravity commented 1 year ago

Given this example:

import { createInterprocess } from '../interprocess';

export const { ipcMain: utilsIpcMain, ipcRenderer: utilsIpcRenderer } = createInterprocess({
  main: {
    /**
     * Returns the electron application version
     */
    async getElectronVersion() {
      return '1.0.0';
    },
  },
  renderer: {
    /**
     * Prints a log message in the frontend console.
     * The first parameter is always an IPC event object.
     */
    async logMessage(_, _msg: string) {
      return;
    },
  },
});

If I want to implement the invoker directly to the context bridge:

import { contextBridge } from 'electron';
import { utilsIpcRenderer } from '../ipc/handlers/utils.ipc';

const api = {
  utils: {
    // typescript complains about the first parameter even though the handler is defined without any params
    // so I have to satisfy it by using undefined
    getElectronVersion: () => utilsIpcRenderer.invoke.getElectronVersion(undefined),
    onLogMessage: (callback) => {
      utilsIpcRenderer.handle.logMessage(async (_, { data: msg }) => {
        callback(msg);
      });
    },
  },
};

try {
  contextBridge.exposeInMainWorld('native', api);
} catch (error) {
  console.error(error);
}
image
daltonmenezes commented 1 year ago

Hi @theogravity , could you please test the #8 PR?