type MainWindowLike = { isDestroyed: () => boolean } type EnsureMainWindowOptions = { isReady: boolean createWindow: () => unknown focusWindow: (window: T) => unknown focusExisting?: boolean } export function ensureMainWindow( window: T | null | undefined, { isReady, createWindow, focusWindow, focusExisting = true }: EnsureMainWindowOptions ) { if (!window || window.isDestroyed()) { // a closed electron window stays truthy, so replace it before invoking native methods. if (isReady) { createWindow() } return } if (focusExisting) { focusWindow(window) } }