Interface for Native Module in Electron-TypeScript-React App

292 Views Asked by At

I'm writing an app that depends on a native Node module using Electron, React and TypeScript. I have context isolation on and am using a preload script to expose the API of the native module onto the global scope. My preload script looks like:

const {contextBridge} = require('electron')

contextBridge.exposeInMainWorld('electron', {basis: require('basis')})

In a non-typescript version of my App I can then use it by saying:

window.electron.basis["some property of the native module"]

However, TypeScript obviously has no notion of the API from the native module. How should I make it aware? The one success I had was in my index.tsx putting:

declare global {
    interface Window {
        electron: { basis: { greeting(): string } };
    }
}

But that seems clunky. Should I put a .d.ts file in the native module? Can I export this interface merging to a different file? For reference I am using Neon with the N-API backend.

0

There are 0 best solutions below