0
votes

I am following the example here and have this TypeScript code:

const { app, BrowserWindow, ipcMain } = require('electron');

let win;

function createWindow () {
  // Create the browser window.
  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // and load the index.html of the app.
  win.loadFile('bin/js-debug/index.html')

  // Open the DevTools.
  //win.webContents.openDevTools()

  // Emitted when the window is closed.
  win.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  })
}

I changed the extension from .js to .ts and it started to show errors.

I get this warning:

Variable 'win' implicitly has type 'any' in some locations where its type cannot be determined.ts(7034)

So I tried to add the type like so:

let win:BrowserWindow;

and I get this message:

'BrowserWindow' refers to a value, but is being used as a type here.

NOTE:

If I set the type to any the error goes away.

let win:any;
1
What you are seeing aren't strictly errors, more so type safety checks. How do you import browser window?Avin Kavish
With const { app, BrowserWindow, ipcMain } = require('electron');1.21 gigawatts

1 Answers

1
votes

When using typescript, you have to use the typescript module import syntax (similar to ESModule import), else typescript will not import the types and see BrowserWindow as a variable defined through require()

import { app, BrowserWindow, ipcMain } from 'electron'