diff --git a/lib/browser/remote/server.ts b/lib/browser/remote/server.ts index 4c49385c380d..c790320eba73 100644 --- a/lib/browser/remote/server.ts +++ b/lib/browser/remote/server.ts @@ -23,7 +23,7 @@ const FUNCTION_PROPERTIES = [ // The remote functions in renderer processes. // id => Function -const rendererFunctions = v8Util.createDoubleIDWeakMap() +const rendererFunctions = v8Util.createDoubleIDWeakMap<(...args: any[]) => void>() type ObjectMember = { name: string, @@ -289,7 +289,7 @@ const unwrapArgs = function (sender: electron.WebContents, frameId: number, cont case 'function': { // Merge contextId and meta.id, since meta.id can be the same in // different webContents. - const objectId = [contextId, meta.id] + const objectId: [string, number] = [contextId, meta.id] // Cache the callbacks in renderer. if (rendererFunctions.has(objectId)) { @@ -373,12 +373,12 @@ const logStack = function (contents: electron.WebContents, code: string, stack: } handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, contextId, passedContextId, id) { - const objectId = [passedContextId, id] + const objectId: [string, number] = [passedContextId, id] if (!rendererFunctions.has(objectId)) { // Do nothing if the error has already been reported before. return } - removeRemoteListenersAndLogWarning(event.sender, rendererFunctions.get(objectId)) + removeRemoteListenersAndLogWarning(event.sender, rendererFunctions.get(objectId)!) }) handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName, stack) { diff --git a/spec-main/api-menu-spec.ts b/spec-main/api-menu-spec.ts index 1dafc6d1a1a7..98ff5f4b1aed 100644 --- a/spec-main/api-menu-spec.ts +++ b/spec-main/api-menu-spec.ts @@ -829,7 +829,7 @@ describe('Menu module', function () { // Keep a weak reference to the menu. const v8Util = process.electronBinding('v8_util') - const map = (v8Util as any).createIDWeakMap() as any + const map = v8Util.createIDWeakMap() map.set(0, menu) setTimeout(() => { @@ -839,7 +839,7 @@ describe('Menu module', function () { setTimeout(() => { // Try to receive menu from weak reference. if (map.has(0)) { - map.get(0).closePopup() + map.get(0)!.closePopup() done() } else { done('Menu is garbage-collected while popuping') diff --git a/typings/internal-ambient.d.ts b/typings/internal-ambient.d.ts index 53dabf6f22c9..25d98a21671a 100644 --- a/typings/internal-ambient.d.ts +++ b/typings/internal-ambient.d.ts @@ -29,7 +29,8 @@ declare namespace NodeJS { setHiddenValue(obj: any, key: string, value: T): void; deleteHiddenValue(obj: any, key: string): void; requestGarbageCollectionForTesting(): void; - createDoubleIDWeakMap(): any; + createIDWeakMap(): ElectronInternal.KeyWeakMap; + createDoubleIDWeakMap(): ElectronInternal.KeyWeakMap<[string, number], V>; setRemoteCallbackFreer(fn: Function, frameId: number, contextId: String, id: number, sender: any): void } diff --git a/typings/internal-electron.d.ts b/typings/internal-electron.d.ts index e8db2167c650..be448177d540 100644 --- a/typings/internal-electron.d.ts +++ b/typings/internal-electron.d.ts @@ -112,6 +112,13 @@ declare namespace ElectronInternal { appIcon: string | null; } + interface KeyWeakMap { + set(key: K, value: V): void; + get(key: K): V | undefined; + has(key: K): boolean; + remove(key: K): void; + } + // Internal IPC has _replyInternal and NO reply method interface IpcMainInternalEvent extends Omit { _replyInternal(...args: any[]): void;