From 47d1005798f3feb2ca8f86a93f8902609a83b427 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 fbad94431..87ac23cc7 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 @@ -111,30 +121,13 @@ export interface SpectronClient extends WebdriverIO.Browser<'async'> { ): 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 = { /** @@ -254,7 +247,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: typeof Electron; + 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.