The Electron Performance documentation advises against loading and running code too soon. It suggests using the strategy of deferring the loading of sizable modules until they are actually needed, rather than placing all require()
statements at the top of the file as is common in traditional Node.js development.
Although this approach is recommended for optimal performance, it presents a challenge for TypeScript users working with Electron applications like electron-react-boilerplate
. An example in src/main.dev.ts
triggers an ESLint error 'Unsafe call of an any typed value. eslint(
)' when using @typescript-eslint/no-unsafe-call
require('electron-debug')
.
However, replacing require
with import
eliminates this issue:
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 });
}
But the question remains - how can one achieve the same result while still utilizing require
with proper types support? Options like
import module = require('module')
have been suggested, but implementing them within certain contexts leads to other TypeScript errors.
Looking for solutions, some references such as this answer provide insights but do not offer a definitive resolution for integrating require
seamlessly with TypeScript's type checking system.