如何在 Electron 上使用具有 TypeScript 类型支持的内部函数或条件?

问题描述

Electron Performance docs中,如下所述:

在传统的 Node.js 开发中,我们习惯于将所有 require() 语句放在顶部。如果您目前正在使用相同的策略编写 Electron 应用程序,并且正在使用您并不立即需要的大量模块,请应用相同的策略并将加载推迟到更合适的时间。

因此建议“及时”分配资源,在需要时调用 require()。问题是我正在尝试使用 electron-react-boilerplate 并且 TypeScript 似乎不能很好地支持这种代码。下面是一个例子:

src/main.dev.ts

if (
  process.env.NODE_ENV === 'development' ||
  process.env.DEBUG_PROD === 'true'
) {
  require('electron-debug')({ showDevTools: false });
}

它在 @typescript-eslint/no-unsafe-call 中有一个 ESLint 错误“Unsafe call of an any typed value.eslint(require('electron-debug'))”,即使 this library has types defined。 >

但是如果用 import 替换它,它就不会出现错误:

import electronDebug from 'electron-debug'; // at the top of the file

if (
  process.env.NODE_ENV === 'development' ||
  process.env.DEBUG_PROD === 'true'
) {
  electronDebug({ showDevTools: false });
}

那么在这种情况下如何使用 require 并支持类型?

我读到 this answer 说要使用 import module = require('module'),但随后出现错误,提示“导入声明只能在命名空间或模块中使用。ts(1232)”如果我在 if 或函数中使用它。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)