【问题标题】:How to correct error win refers to a value, but is being used as a type here如何更正错误 win 指的是一个值,但在这里被用作一个类型
【发布时间】:2019-06-09 05:15:51
【问题描述】:

我正在关注示例 here 并拥有以下 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;

我收到这条消息:

'BrowserWindow' 指的是一个值,但在这里被用作一个类型。

注意:

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

let win:any;

【问题讨论】:

  • 您所看到的并非严格意义上的错误,更多的是类型安全检查。如何导入浏览器窗口?
  • const { app, BrowserWindow, ipcMain } = require('electron');

标签: typescript electron


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-12
    • 2020-03-23
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2020-12-22
    • 2021-08-02
    • 2021-07-07
    相关资源
    最近更新 更多