简体   繁体   English

无法使用TypeScript的remote.require()

[英]Unable to use remote.require() with TypeScript

I'm developing an Electron application in TypeScript, and I've run into a frustrating issue. 我正在使用TypeScript开发一个Electron应用程序,但我遇到了一个令人沮丧的问题。

I'm attempting to run a module from a renderer process with the following code: 我正在尝试使用以下代码从渲染器进程运行模块:

import { remote } from 'electron'

const myModule = remote.require('./my-module')

The only way I can use remote.require with my module, is by adding module.exports = myModule to the my-module.ts file. 我可以使用的唯一方法remote.require与我的模块,是通过添加module.exports = myModulemy-module.ts文件。

// my-module.ts
export class myModule { ... }

module.exports = myModule

After adding the module.exports = myModule line, it causes errors where ever I import myModule with a standard TypeScript import. 添加module.exports = myModule行后,只要我使用标准TypeScript导入导入myModule,就会导致错误。

For example 例如

// main.ts
import { myModule } from './my-module'
// Error here

To fix this error, I can simply replace the import statement with 要修复此错误,我只需将import语句替换为

const myModule = require('./my-module')

But when I replace the import statement to the one above, my TypeScript does not recognise myModule and all it's members. 但是当我将import语句替换为上面的语句时,我的TypeScript无法识别myModule及其所有成员。 The intelisense shows nothing for myModule and TypeScript throws a bunch of errors even though the program runs just fine. 对于myModule,intelisense没有显示任何内容,即使程序运行正常,TypeScript myModule引发一堆错误。


Some things to note: 有些事情需要注意:

  1. The contents of my tsconfig.json are: 我的tsconfig.json的内容是:

     { "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "outDir": "./", "emitDecoratorMetadata": true, "experimentalDecorators": true, "alwaysStrict": true, }, "exclude": [ "node_modules" ] } 
  2. In the compiled JavaScript code, it results in something like the following when used with a TypeScript import statement: 在编译的JavaScript代码中,当与TypeScript导入语句一起使用时,会产生类似下面的内容:

     var my_module_1 = require("./my-module"); my_module_1.myModule.foo('bar'); 

    Which gives me an error of TypeError: Cannot read property 'sayHi' of undefined . 这给了我一个TypeError: Cannot read property 'sayHi' of undefined的错误TypeError: Cannot read property 'sayHi' of undefined

    The JavaScript should look like this instead for it to work: JavaScript应该看起来像这样工作:

     var my_module_1 = require("./my-module"); my_module_1.foo('bar'); 

How can I use remote.require() with TypeScript? 如何在TypeScript中使用remote.require()

When you implement your code in ESM and import it in commonjs (that's what remote.require() does), you will get the module namespace object: 当您在ESM中实现代码并将其导入commonjs(这就是remote.require()所做的)时,您将获得模块命名空间对象:

// myModule.ts
export function foo() { ... }

// and to access the default export:
export default const boo = 'boo'

// consumer.js
const myModule = require('./myModule')

myModule = { foo() { ... }, default: 'boo' }

Therefore, your to use your myModule class, you need to do this: 因此,您要使用myModule类,您需要这样做:

const myModule = remote.require('./myModule')

const obj = new myModule.myModule()
const boo = myModule.default

UPDATE: remote.require() is a dynamic call, so TypeScript can't detect the type of the myModule and set it to any . 更新: remote.require()是一个动态调用,因此TypeScript无法检测myModule的类型并将其设置为any

You can do the following to get the type back: 您可以执行以下操作以获取类型:

import * as MyModule from './myModule'
const myModule: typeof MyModule = remote.require('./myModule')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM