Skip to content

Commit 3a1557d

Browse files
committed
Changed declare module "electron" to export = Electron
and added const declarations so that `app` etc are accessible.
1 parent 54fcd2a commit 3a1557d

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

base/base_footer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
declare module 'electron' {
2-
const electron: Electron.AllElectron;
3-
export = electron;
2+
export = Electron;
43
}
54

65
interface NodeRequireFunction {
7-
(moduleName: 'electron'): Electron.AllElectron;
6+
(moduleName: 'electron'): typeof Electron;
87
}
98

109
interface File {

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const wrapWithHeaderAndFooter = (outputLines, electronVersion) => {
1818
const newOutputLines = []
1919
utils.extendArray(newOutputLines, fs.readFileSync(path.resolve(__dirname, 'base/base_header.ts'), 'utf8').replace('<<VERSION>>', electronVersion).split(/\r?\n/))
2020

21+
newOutputLines.push('/** @deprecated */')
2122
newOutputLines.push('declare namespace Electron {')
2223
utils.extendArray(newOutputLines, fs.readFileSync(path.resolve(__dirname, 'base/base_inner.ts'), 'utf8').replace('<<VERSION>>', electronVersion).split(/\r?\n/))
2324

lib/master-interfaces.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = (API, addToOutput) => {
88
const CommonInterface = ['interface CommonInterface {']
99
const MainInterface = ['interface MainInterface extends CommonInterface {']
1010
const RendererInterface = ['interface RendererInterface extends CommonInterface {']
11-
const ElectronMainAndRendererInterface = ['interface AllElectron {']
11+
const ElectronMainAndRendererInterface = ['interface AllElectron extends MainInterface, RendererInterface {}']
12+
const constDeclarations = []
1213
const EMRI = {}
1314

1415
const classify = (moduleName) => {
@@ -28,11 +29,20 @@ module.exports = (API, addToOutput) => {
2829
if (module.name === 'process') return
2930
let TargetInterface
3031
const isClass = module.type === 'Class' || API.some((tModule, tIndex) => index !== tIndex && tModule.name.toLowerCase() === module.name.toLowerCase())
31-
const moduleString = ` ${classify(module.name)}: ${isClass ? 'typeof ' : ''}Electron.${_.upperFirst(module.name)}`
32+
const moduleString = ` ${classify(module.name)}: ${isClass ? 'typeof ' : ''}${_.upperFirst(module.name)}`
3233
if (!module.process) {
3334
// We must be a structure or something
3435
return
3536
}
37+
if (!isClass || module.name != classify(module.name)) {
38+
if (isClass) {
39+
constDeclarations.push(
40+
`type ${classify(module.name)} = ${_.upperFirst(module.name)};`,
41+
`const ${classify(module.name)}: typeof ${_.upperFirst(module.name)};`)
42+
} else {
43+
constDeclarations.push(`const ${classify(module.name)}: ${_.upperFirst(module.name)};`)
44+
}
45+
}
3646
if (module.process.main && module.process.renderer) {
3747
TargetInterface = CommonInterface
3848
} else if (module.process.main) {
@@ -46,7 +56,6 @@ module.exports = (API, addToOutput) => {
4656
if (TargetInterface) {
4757
debug(classify(module.name).toLowerCase(), EMRI[classify(module.name).toLowerCase()])
4858
if (!EMRI[classify(module.name).toLowerCase()]) {
49-
ElectronMainAndRendererInterface.push(moduleString)
5059
TargetInterface.push(moduleString)
5160
}
5261
EMRI[classify(module.name).toLowerCase()] = true
@@ -56,11 +65,11 @@ module.exports = (API, addToOutput) => {
5665
CommonInterface.push('}')
5766
MainInterface.push('}')
5867
RendererInterface.push('}')
59-
ElectronMainAndRendererInterface.push('}')
6068

6169
addToOutput([''])
6270
addToOutput(CommonInterface, ';')
6371
addToOutput(MainInterface, ';')
6472
addToOutput(RendererInterface, ';')
6573
addToOutput(ElectronMainAndRendererInterface, ';')
74+
addToOutput(constDeclarations)
6675
}

test-smoke/electron/test/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
session,
2424
systemPreferences,
2525
webContents,
26+
Event,
2627
} from "electron";
2728

2829
import * as path from "path";
@@ -33,6 +34,7 @@ import * as path from "path";
3334
// Keep a global reference of the window object, if you don't, the window will
3435
// be closed automatically when the javascript object is GCed.
3536
let mainWindow: Electron.BrowserWindow = null;
37+
const mainWindow2: BrowserWindow = null;
3638

3739
// Quit when all windows are closed.
3840
app.on("window-all-closed", () => {
@@ -524,6 +526,11 @@ ipcMain.on("synchronous-message", (event: Electron.Event, arg: any) => {
524526
event.returnValue = "pong";
525527
});
526528

529+
ipcMain.on("synchronous-message", (event: Event, arg: any) => {
530+
console.log("event isn't namespaced and refers to the correct type.");
531+
event.returnValue = "pong";
532+
});
533+
527534
const winWindows = new BrowserWindow({
528535
width: 800,
529536
height: 600,

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"no-namespace": false,
1414
"quotemark": false,
1515
"unified-signatures": false,
16-
"variable-name": false
16+
"variable-name": false,
17+
"no-shadowed-variable": false
1718
}
1819
}

0 commit comments

Comments
 (0)