Electron has two main types of processes: the main process and renderer processes.
TypeScript adds static typing to JavaScript. For example, instead of using a variable without a clear type like this in JavaScript:
let num = 10;
In TypeScript, you can explicitly define the type:
let num: number = 10;
This helps catch type - related errors at compile - time rather than at runtime.
The Electron TypeScript Starter combines the power of Electron and TypeScript. It typically includes a build system (like Webpack or Parcel) to compile TypeScript code into JavaScript and a development server for hot - reloading during development.
electron - typescript - boilerplate
:git clone https://github.com/alex8088/electron-typescript-boilerplate.git my - electron - app
cd my - electron - app
npm install
The typical project structure of an Electron TypeScript Starter might look like this:
my - electron - app/
├── src/
│ ├── main/
│ │ └── main.ts # Main process code
│ └── renderer/
│ └── renderer.ts # Renderer process code
├── public/
│ └── index.html # HTML file for the renderer process
├── package.json
├── tsconfig.json # TypeScript configuration
└── webpack.config.js # Webpack configuration (if using Webpack)
src/main/main.ts
)import { app, BrowserWindow } from 'electron';
let mainWindow: BrowserWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('public/index.html');
mainWindow.on('closed', function () {
mainWindow = null;
});
}
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all - closed', function () {
if (process.platform!== 'darwin') app.quit();
});
src/renderer/renderer.ts
)// Access the DOM
const button = document.createElement('button');
button.textContent = 'Click me';
document.body.appendChild(button);
button.addEventListener('click', () => {
alert('Button clicked!');
});
Development Mode
npm run dev
This will start the development server with hot - reloading.
Production Build
npm run build
This will compile the TypeScript code and create a distributable version of the application.
In both the main and renderer processes, it’s important to handle errors properly. For example, in the main process, you can handle errors when creating the browser window:
function createWindow() {
try {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('public/index.html');
} catch (error) {
console.error('Error creating window:', error);
}
}
Electron provides the ipcMain
and ipcRenderer
modules for communication between the main and renderer processes.
import { app, BrowserWindow, ipcMain } from 'electron';
ipcMain.on('message - from - renderer', (event, arg) => {
console.log('Received message from renderer:', arg);
event.sender.send('message - from - main', 'Message received');
});
import { ipcRenderer } from 'electron';
ipcRenderer.send('message - from - renderer', 'Hello from renderer');
ipcRenderer.on('message - from - main', (event, arg) => {
console.log('Received message from main:', arg);
});
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
});
The Electron TypeScript Starter is a powerful tool for building cross - platform desktop applications. By combining the capabilities of Electron and TypeScript, developers can create robust, maintainable, and secure applications. Understanding the fundamental concepts, following the usage methods, and adopting common and best practices will help you make the most out of this technology stack.