我有一个 Electron 应用程序,它使用 create-react-app
和 TypeScript 配置(它使用 Babel 将 TypeScript 代码编译为 JavaScript)。
在这种情况下,在导入访问文件系统的模块(如 electron-store
)时,我遵循了使用 window.require
的建议,以避免与 Babel 的 冲突>require
(这会出错,例如 fs.whatever not found
)。
我的问题是这些模块以 any
的形式输入,即使输入信息可用也是如此。我如何将 TypeScript 纳入使用 window.require
导入的模块的类型信息?
具体来说,这里有一个例子:
const Store = window.require('electron-store');
type StoreType = {
'root-path': string;
};
// fails with "Untyped function calls may not accept type arguments."
export const store = new Store<StoreType>();
最佳答案
这不能通用地完成,也就是说对于任意模块说明符,但我怀疑只有少数模块会以这种方式加载。鉴于此,您最好的选择是编写类似
的内容 // augmentations.d.ts or whatever you want to call it.
import ElectronStore from 'electron-store';
declare global {
interface Window {
require(moduleSpecifier: 'electron-store'): typeof ElectronStore;
require(moduleSpecifier: 'some-specifier'): SomeType;
}
}
关于javascript - 将 window.require 与 Electron 和 TypeScript 与 create-react-app 结合使用时缺少输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58421539/