Reputation: 1448
I'm trying to specify some overrides for events that will give me type safety in an electron app.
For my types:
export type SectionName = 'webview'
| 'index'
| 'settings'
export interface ShowSection {
key: SectionName,
}
I want to augment this code:
import {
ipcMain,
} from 'electron'
ipcMain.on('rendered', (event) => {
const sender = event.sender
event.sender.send('show-section', {
key: 'index',
})
})
The sender is of type Electron.WebContents
which is defined here
I've tried to augment like this:
declare namespace Electron {
interface WebContents extends NodeJS.EventEmitter {
send(channel: 'show-section', arg: ShowSection): void;
}
}
I'm not sure how to do this so that I can get type safety on individual events.
Thanks
Upvotes: 0
Views: 516
Reputation: 12414
The correct syntax to augment the WebContents
interface is:
declare global {
namespace Electron {
interface WebContents extends NodeJS.EventEmitter {
send(channel: 'show-section', arg: ShowSection): void;
}
}
}
After this you will see the overload options over the send(...)
:
UPDATE
You can put this on your declaration file (e.g. custom-typings.d.ts):
export interface ShowSection {
key: SectionName
}
export type SectionName =
'webview'
| 'index'
| 'settings'
declare global {
namespace Electron {
interface WebContents extends NodeJS.EventEmitter {
send(channel: 'show-section', arg: ShowSection): void;
}
}
}
Upvotes: 1