@electron-toolkit/typed-ipc

中文文档
1.0.2发布于 2024/11/11MITTypeScriptESM + CJS零依赖近 30 天下载 1.5万

Type-safe Electron IPC, best practices in TypeScript.

原始使用文档
文档目录4 节
  1. Usage
  2. Install
  3. Get Started
  4. License

@electron-toolkit/typed-ipc

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.

Usage

Install

npm i @electron-toolkit/preload @electron-toolkit/typed-ipc

Get Started

  1. 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()
    
  2. 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]
    }
    
  3. 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'
    })
    
  4. 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)
    })
    

License

MIT © alex.wei

版本与发布记录

@electron-toolkit/typed-ipc 共发布 3 个版本,首次发布于 2024/08/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2024/11/112 年前
1.0.12024/08/262 年前
1.0.02024/08/242 年前

关于 @electron-toolkit/typed-ipc 的常见问题

@electron-toolkit/typed-ipc 是什么?

Type-safe Electron IPC, best practices in TypeScript.

如何安装 @electron-toolkit/typed-ipc?

在项目目录运行 npm install @electron-toolkit/typed-ipc,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:electron(>=13.0.0)、@electron-toolkit/preload(>=3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@electron-toolkit/typed-ipc 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@electron-toolkit/typed-ipc 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@electron-toolkit/typed-ipc 可以商用吗?

@electron-toolkit/typed-ipc 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@electron-toolkit/typed-ipc 的下载量有多少?

根据 npm 官方下载统计,@electron-toolkit/typed-ipc 在 2026/10/14 – 2026/11/12 这 30 天内被下载 14,654 次,日均约 488 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@electron-toolkit/typed-ipc 还在维护吗?

最新版本 1.0.2 发布于 2024/11/11(2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。