Type-safe Electron IPC, best practices in TypeScript.
Type checking and intellisense are available both in the main process and the renderer process. Both the listener parameters, handler parameters and return value types can be inspected and sensed, and you will not miss any modifications.
It provide a good development experience. Constrain the IPC registration of the main process and the calling of the renderer process, and avoid low-level errors. At the same time, it maintains the original writing method of IPC, which is more conducive to reading and understanding rather than over-encapsulating Electorn's IPC.
npm i @electron-toolkit/preload @electron-toolkit/typed-ipc
-
Use @electron-toolkit/preload to expose Electron APIs.
You can expose it in the specified preload script:
import { contextBridge } from 'electron' import { electronAPI } from '@electron-toolkit/preload' if (process.contextIsolated) { try { contextBridge.exposeInMainWorld('electron', electronAPI) } catch (error) { console.error(error) } } else { window.electron = electronAPI }
or
import { exposeElectronAPI } from '@electron-toolkit/preload' exposeElectronAPI()
-
Add
*.d.ts
declaration file to define IPC event type constraints. And remember to include it in your tsconfig to make sure it takes effect.// Main process ipc events type IpcEvents = | { ping: [string] // listener event map } | { 'say-hello': () => string // handler event map } //Renderer ipc events type IpcRendererEvent = { ready: [boolean] }
-
Register a listener or handler in the main process, or send a message to the renderer.
import { IpcListener, IpcEmitter } from '@electron-toolkit/typed-ipc/main' const ipc = new IpcListener<IpcEvents>() const emitter = new IpcEmitter<IpcRendererEvent>() ipc.on('ping', (e, arg) => { console.log(arg) emitter.send(e.sender, 'ready', true) }) ipc.handle('say-hello', () => { return 'hello' })
-
Send a message from the render process to the main process, or listen for messages from the main process.
import { IpcListener, IpcEmitter } from '@electron-toolkit/typed-ipc/renderer' const ipc = new IpcListener<IpcRendererEvent>() const emitter = new IpcEmitter<IpcEvents>() ipc.on('ready', (e, arg) => { console.log(arg) }) emitter.send('ping', 'pong') emitter.invoke('say-hello').then((str) => { console.log(str) })
MIT © alex.wei