Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 16, 2025

Adds TypeScript tooling and migrates main process and core utility modules to provide type safety for IPC boundaries, Electron APIs, and image processing operations.

Changes

Tooling & Build

  • Added TypeScript compiler with allowJs: true for incremental migration
  • Configured output to dist/, updated main entry point and Electron Builder config
  • Removed outdated @types/electron@1.4.38, using Electron 37's built-in types instead

Type Definitions

  • Centralized IPC channel names and payload types in types/ipc.ts
  • Defined type-safe interfaces for context menus, dialogs, and layer events

File Migrations (JS → TS)

  • main.ts: Typed Electron APIs (IpcMainEvent, BrowserWindow, MenuItemConstructorOptions)
  • core/pix.ts: Typed PIX format with Sharp library interfaces, fixed channels type constraint
  • utils/ipc_bridge.ts: Generic IPC forwarder with typed handlers
  • features/image_mode.ts: Converted to enum with typed mode manager class

Key Technical Details

// Before: untyped dialog calls
dialog.showOpenDialog(...).then(result => {
  event.sender.send("openImgCMD", result.filePaths);
});

// After: typed with proper async/await
const result = await dialog.showOpenDialog({...});
if (!result.canceled) {
  event.sender.send("openImgCMD", result.filePaths);
}

Sharp library type fix:

// Channels must be union type, not number
raw: { width, height, channels: channels as 1 | 2 | 3 | 4 }

All TypeScript files compile to CommonJS in dist/ maintaining compatibility with existing JS modules. Remaining files continue to work via allowJs: true and can be migrated incrementally.

Original prompt

This section details on the original issue you should resolve

<issue_title>Migration to typescript</issue_title>
<issue_description>Summary

Migrate Pegasus Electron app from JavaScript to TypeScript incrementally to improve type safety, DX, and maintainability while minimizing disruption.
Motivation

Strong typing for IPC contracts and render/main boundaries.
Catch runtime errors earlier; safer refactors as features grow.
Improve discoverability with IDE tooling and types.
Scope

Add TypeScript toolchain and config.
Compile to dist and point Electron to compiled outputs.
Incremental file-by-file migration with allowJs.
Provide type definitions for Electron, Node, and frequently used libs.
Keep current security model (no preload refactor yet).
Out of Scope

Security model changes (e.g., contextIsolation: true, preload API).
Large architectural changes or bundler switch.
Test framework introduction.
Plan

Phase 1: Tooling + Build
Phase 2: Type-safe IPC contracts
Phase 3: Core + Utils migration
Phase 4: Main/Renderer migration per panel
Phase 5: Strict mode tightening and cleanup
Tasks

Tooling
Add TypeScript: npm i -D typescript @types/node @types/electron
Add tsconfig.json with outDir: dist, rootDir aligned to repo, allowJs: true, resolveJsonModule: true.
Update package.json:
main → dist/main.js
Scripts: build:ts, watch:ts, dev (tsc watch + electron)
Build Pathing
Ensure assets and pages resolve correctly from compiled output.
Verify Electron Builder targets use compiled dist/main.js.
IPC Types
Centralize IPC channel names in a shared type module (e.g., types/ipc.ts).
Type IPCBridge methods and payloads.
Incremental Migration
Core: pix.js → core/pix.ts, image_layer.js → image_layer.ts, image_renderer.js → image_renderer.ts
Utils: ipc_bridge.js → utils/ipc_bridge.ts
Features: image_mode.js → features/image_mode.ts (enum + class typing)
Processing: image_processor.js → processing/image_processor.ts (define image buffers/types)
Main: main.js → main.ts (Electron types, IPC handlers)
Renderer: renderer/*.js per panel → .ts (DOM and IPC typing)
Config Tweaks
Add paths aliases if needed; ensure relative imports still resolve after build.
Add ESLint with TS plugin optionally for consistency.
CI/Build
Ensure CI runs npm run build:ts before packaging.
Verify Windows packaging still picks compiled files.
Acceptance Criteria

npm run dev launches the app via compiled dist/main.js.
All existing features work (open/save, panels, filters, paint, rotate, crop, analysis).
No console TS errors; baseline noImplicitAny tolerated initially.
IPC channels and payloads typed in a shared module and used on both sides.
Packaging succeeds and launches on Windows.
Risks & Mitigations

Path/asset resolution from dist: keep relative loadFile paths stable; adjust __dirname usage or copy assets.
Third-party types missing: add minimal ambient typings as needed.
Large diff: migrate per-folder, merge behind feature branch typescript, frequent small PRs.
Milestones

M1: Tooling + main.ts compiles/runs.
M2: Core + Utils migrated.
M3: IPC typed + 2 key renderers migrated (resize, filter).
M4: Remaining renderers migrated.
M5: Enable stricter TS options (noImplicitAny, strictNullChecks).
Try It (local)

Install
npm i -D typescript @types/node @types/electron
Scripts (example)
build:ts: tsc -p tsconfig.json
watch:ts: tsc -w -p tsconfig.json
dev: run npm run watch:ts and electron . (main points to dist/main.js)
Not...


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 16, 2025 13:24
Co-authored-by: volta2030 <65960990+volta2030@users.noreply.github.com>
Co-authored-by: volta2030 <65960990+volta2030@users.noreply.github.com>
Copilot AI changed the title [WIP] Migrate Pegasus Electron app from JavaScript to TypeScript Migrate Electron app from JavaScript to TypeScript Dec 16, 2025
Copilot AI requested a review from volta2030 December 16, 2025 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migration to typescript

2 participants