-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypings.d.ts
46 lines (33 loc) · 1.44 KB
/
typings.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/// <reference types="electron" />
interface IpcMethods {
getVersion(): string;
getElectronVersion(): string;
toggleDevTools(): boolean;
writeUserDataFile(fileName: string, fileContent: string): string;
}
interface Window {
electronBridge?: {
isElectronDebug: boolean;
invokeIpc<K extends keyof IpcMethods>(channel: K, ...args: Parameters<IpcMethods[K]>): Promise<IpcResult<ReturnType<IpcMethods[K]>>>;
/** @deprecated Please use explict signatures defined in IpcMethods */
invokeIpc<T = unknown>(channel: string, ...args: Array<any>): Promise<IpcResult<T>>;
}
}
type IpcResult<T> = IpcSuccessResult<T> | IpcFailResult<T>
interface IpcSuccessResult<T> {
ok: true;
data: T;
}
interface IpcFailResult<T> {
ok: false;
data: T | null;
message?: string;
}
type TsParameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
declare namespace Electron {
interface IpcMain {
handle<K extends keyof IpcMethods>(channel: K, listener: (event: Electron.IpcMainInvokeEvent, ...args: TsParameters<IpcMethods[K]>) => Promise<IpcResult<ReturnType<IpcMethods[K]>>> | IpcResult<ReturnType<IpcMethods[K]>>): void;
/** @deprecated Please use explict signatures defined in IpcMethods */
handle(channel: string, listener: (event: Electron.IpcMainInvokeEvent, ...args: Array<any>) => Promise<IpcResult<unknown>> | IpcResult<unknown>): void;
}
}