Skip to content

Commit d54b176

Browse files
committed
Fix types of wrapped Electron methods to return Promise
Fixes #878, #998. Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 6584a0d commit d54b176

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

lib/spectron.d.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
import * as Electron from 'electron';
88
import * as WebdriverIO from 'webdriverio';
99

10+
type MethodsOf<T> = {
11+
[K in keyof T]: T[K] extends (...args: any) => any ? K : never;
12+
}[keyof T];
13+
14+
type MakeAsync<T> = T extends (...args: infer A) => infer R
15+
? (...args: A) => Promise<R extends PromiseLike<infer T> ? T : R>
16+
: never;
17+
18+
type MakeMethodsAsync<T> = { [K in MethodsOf<T>]: MakeAsync<T[K]> };
19+
1020
interface AccessibilityAuditOptions {
1121
/**
1222
* true to ignore failures with a severity of 'Warning' and only
@@ -117,26 +127,13 @@ export interface SpectronClient extends WebdriverIO.BrowserObject {
117127
): Promise<AccessibilityAuditResult>;
118128
}
119129

120-
export interface SpectronWindow extends Electron.BrowserWindow {
121-
capturePage(): Promise<Electron.NativeImage>;
122-
}
130+
export type SpectronElectron = {
131+
[K in keyof typeof Electron]: MakeMethodsAsync<typeof Electron[K]>;
132+
};
123133

124-
export interface SpectronWebContents extends Electron.WebContents {
125-
savePage(
126-
fullPath: string,
127-
saveType: 'HTMLOnly' | 'HTMLComplete' | 'MHTML',
128-
callback?: (eror: Error) => void
129-
): boolean;
130-
savePage(
131-
fullPath: string,
132-
saveType: 'HTMLOnly' | 'HTMLComplete' | 'MHTML'
133-
): Promise<void>;
134-
savePage(
135-
fullPath: string,
136-
saveType: 'HTMLOnly' | 'HTMLComplete' | 'MHTML'
137-
): any;
138-
executeJavaScript(code: string, userGesture?: boolean): Promise<any>;
139-
}
134+
export type SpectronWindow = MakeMethodsAsync<Electron.BrowserWindow>;
135+
136+
export type SpectronWebContents = MakeMethodsAsync<Electron.WebContents>;
140137

141138
type BasicAppSettings = {
142139
/**
@@ -256,7 +253,7 @@ export class Application {
256253
* Each Electron module is exposed as a property on the electron property so you can
257254
* think of it as an alias for require('electron') from within your app.
258255
*/
259-
electron: Electron.RemoteMainInterface;
256+
electron: SpectronElectron;
260257
/**
261258
* The browserWindow property is an alias for require('electron').remote.getCurrentWindow().
262259
* It provides you access to the current BrowserWindow and contains all the APIs.

0 commit comments

Comments
 (0)