From 41b44521708cdbf686d7c90722b2d9be4d7e07bb Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 8 Jul 2021 15:23:53 -0700 Subject: [PATCH] Fix types of wrapped Electron methods to return Promise Fixes #878, fixes #998. Signed-off-by: Anders Kaseorg --- lib/spectron.d.ts | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/spectron.d.ts b/lib/spectron.d.ts index caf31ab4f..ca2657893 100644 --- a/lib/spectron.d.ts +++ b/lib/spectron.d.ts @@ -7,6 +7,16 @@ import * as Electron from 'electron'; import * as WebdriverIO from 'webdriverio'; +type MethodsOf = { + [K in keyof T]: T[K] extends (...args: any) => any ? K : never; +}[keyof T]; + +type MakeAsync = T extends (...args: infer A) => infer R + ? (...args: A) => Promise ? T : R> + : never; + +type MakeMethodsAsync = { [K in MethodsOf]: MakeAsync }; + interface AccessibilityAuditOptions { /** * true to ignore failures with a severity of 'Warning' and only @@ -117,30 +127,13 @@ export interface SpectronClient extends WebdriverIO.BrowserObject { ): Promise; } -export type SpectronWindow = { - [P in keyof Electron.BrowserWindow]: Electron.BrowserWindow[P] extends ( - ...args: infer A - ) => infer R - ? (...args: A) => Promise - : undefined; +export type SpectronElectron = { + [K in keyof typeof Electron]: MakeMethodsAsync; }; -export interface SpectronWebContents extends Electron.WebContents { - savePage( - fullPath: string, - saveType: 'HTMLOnly' | 'HTMLComplete' | 'MHTML', - callback?: (error: Error) => void - ): boolean; - savePage( - fullPath: string, - saveType: 'HTMLOnly' | 'HTMLComplete' | 'MHTML' - ): Promise; - savePage( - fullPath: string, - saveType: 'HTMLOnly' | 'HTMLComplete' | 'MHTML' - ): any; - executeJavaScript(code: string, userGesture?: boolean): Promise; -} +export type SpectronWindow = MakeMethodsAsync; + +export type SpectronWebContents = MakeMethodsAsync; type BasicAppSettings = { /** @@ -260,7 +253,7 @@ export class Application { * Each Electron module is exposed as a property on the electron property so you can * think of it as an alias for require('electron') from within your app. */ - electron: Electron.RemoteMainInterface; + electron: SpectronElectron; /** * The browserWindow property is an alias for require('electron').remote.getCurrentWindow(). * It provides you access to the current BrowserWindow and contains all the APIs.