如何纠正错误 win 指的是一个值,但在这里用作类型



我按照这里的例子,有这个TypeScript代码:

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
  })
}

我将扩展名从 .js 更改为 .ts,它开始显示错误。

我收到此警告:

变量 'win' 在某些无法确定其类型的位置隐式具有类型"any"。ts(7034(

所以我尝试添加这样的类型:

let win:BrowserWindow;

我收到这条消息:

"浏览器窗口"指的是一个值,但在这里用作类型。

注意:

如果我将类型设置为任何错误,错误就会消失。

let win:any;

使用打字稿时,必须使用打字稿模块导入语法(类似于ESModule导入(,否则打字稿不会导入类型,并将BrowserWindow视为通过require()定义的变量

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

相关内容

最新更新