Electron is a framework developed by GitHub. It uses Chromium and Node.js to create desktop applications. With Electron, developers can use web technologies to build applications that run on Windows, macOS, and Linux. For example, applications like Visual Studio Code, Slack, and Discord are built using Electron.
Electron Builder is a complete solution to package and distribute your Electron application with “auto update” support out of the box. It can create installers for different platforms, manage application icons, and handle code signing.
TypeScript is a statically typed superset of JavaScript that compiles to plain JavaScript. It adds types to JavaScript, which helps in writing more robust and maintainable code. For example, in JavaScript, a variable can hold any type of value, but in TypeScript, you can define the type of a variable, like let num: number = 10;
mkdir electron - typescript - app
cd electron - typescript - app
npm init -y
npm install electron electron - builder typescript --save - dev
npx tsc --init
In the tsconfig.json
file, you can make the following changes:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
src
directory in your project and add a main.ts
file:import { app, BrowserWindow } from 'electron';
let mainWindow: BrowserWindow | null;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
mainWindow.loadFile('index.html');
mainWindow.on('closed', () => {
mainWindow = null;
});
}
app.whenReady().then(() => {
createWindow();
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window - all - closed', () => {
if (process.platform!== 'darwin') app.quit();
});
index.html
file in the root directory:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device - width, initial - scale = 1.0">
<title>Electron TypeScript App</title>
</head>
<body>
<h1>Hello, Electron with TypeScript!</h1>
</body>
</html>
npx tsc
npx electron dist/main.js
build
script to your package.json
file:{
"scripts": {
"build": "npx tsc && electron - builder"
},
"build": {
"appId": "com.example.electron - typescript - app",
"productName": "Electron TypeScript App",
"directories": {
"output": "dist"
},
"win": {
"target": [
"nsis"
]
},
"mac": {
"target": [
"dmg"
]
},
"linux": {
"target": [
"deb"
]
}
}
}
npm run build
This will create installers for different platforms in the dist
directory.
A common directory structure for an Electron TypeScript project is as follows:
electron - typescript - app/
├── src/
│ ├── main.ts
│ ├── renderer/
│ ├── renderer.ts
├── index.html
├── package.json
├── tsconfig.json
├── dist/
├── main.js
├── renderer.js
├── [platform - specific installers]
npm
or yarn
to manage your project dependencies.package.json
file up - to - date and use version ranges carefully.try {
// Some code that might throw an error
const result = JSON.parse('invalid json');
} catch (error) {
console.error('Error parsing JSON:', error);
}
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});
Combining Electron Builder with TypeScript can significantly improve the development process of cross - platform desktop applications. TypeScript adds static typing to your code, making it more reliable and easier to maintain. Electron Builder simplifies the packaging and distribution of your application. By following the concepts, usage methods, common practices, and best practices outlined in this blog, you can build high - quality Electron applications with TypeScript efficiently.