DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Twelve shapes are declared twice in our Electron app, and the one module both halves import is a single number

Notifio is an Electron app. The main process does the work and the renderer is a web page, and they do not talk over IPC: the main process runs a local HTTP server and the window fetches from 127.0.0.1 like any other client, with an event stream pushing updates the other way.

That means the boundary between the two halves is JSON, and JSON has no types. So each half has its own type file:

$ wc -l src/types.ts src/renderer/types.ts
     350 src/types.ts
     164 src/renderer/types.ts
Enter fullscreen mode Exit fullscreen mode

26 exported declarations on one side, 16 on the other. I went through both to find out how much of that is the same thing written twice, and whether the copies still agree.

The census

declared in src/types.ts only              16
declared in src/renderer/types.ts only      6
declared in both                           10
plus Find        (src/finds.ts      <-> renderer/types.ts)
plus SiteRuntime (src/site-state.ts <-> renderer/types.ts)
                                           --
shapes that exist on both sides            12
Enter fullscreen mode Exit fullscreen mode

The 16 that live only in the main process are the ones the window has no business knowing: the result of a check, what the reply engine did, the on-disk user data, the logging callbacks. The 6 that live only in the renderer are the wire itself, including the SSEMessage union and the per-search runtime state the UI paints.

The 12 in the middle are the interesting ones, because every one of them is a shape that crosses the boundary as JSON and is therefore described twice, by hand, in two files that cannot see each other.

Nine of the twelve agree exactly

Site and Config, the two shapes that make up the saved searches, are the same five and four fields on both sides, in the same order:

// src/types.ts
export interface Site {
  name: string;
  url: string;
  enabled: boolean;
  skipLogin?: boolean;
  /** Defaults to 'email' when absent, so existing configs keep working. */
  replyMode?: ReplyMode;
}
Enter fullscreen mode Exit fullscreen mode
// src/renderer/types.ts
/** A saved search, exactly as it is stored in config.json. */
export interface Site {
  name: string;
  url: string;
  enabled: boolean;
  skipLogin?: boolean;
  /** Absent means "email", the original alert-only behaviour. */
  replyMode?: ReplyMode;
}
Enter fullscreen mode Exit fullscreen mode

SiteRuntime is twelve fields on both sides, same names, same order, same nullability. Find is six. MonitorStatus is the same seven-variant union in both files. Notice, ReplyMode and AutoReplySettings match too. Nine of the twelve shapes are identical, maintained in parallel, by somebody remembering.

The only thing enforcing that is prose. Four declarations in the renderer carry a note:

/** Mirrors SiteRuntime in src/site-state.ts. Keep the two in step. */
/** One listing Notifio found. Mirrors Find in src/finds.ts. */
/** Mirrors MonitorStatus in src/types.ts. Keep the two in step. */
Enter fullscreen mode Exit fullscreen mode

and one declaration in the main process points forward at the renderer. Five comments, covering four of the twelve shapes. The other eight are duplicated silently.

Three differ, and two of the three differences are correct

The three that do not match are the ones describing a recorded reply: FormFieldSchema, RecipeStep and Recipe. In every case the renderer's copy is the smaller one.

shape             main fields   renderer fields   dropped
---------------------------------------------------------
FormFieldSchema       12              9              3
Recipe                15             12              3
RecipeStep            11              5              7 (and 1 added)
Enter fullscreen mode Exit fullscreen mode

Thirteen fields exist on the main side and not in the renderer's description of the same object. That is not drift, it is a narrowing, and it is the right direction: the main process needs to know how to execute a step, and the window needs to list the steps and say whether the whole thing still works. A UI that cannot see the execution detail cannot accidentally depend on it, and because this is JSON over a socket, the extra fields arrive anyway and TypeScript simply does not offer them.

Recipe shows the same thing in miniature. The main process names its status union:

export type RecipeStatus = 'draft' | 'verified' | 'broken';
Enter fullscreen mode Exit fullscreen mode

The renderer inlines the same three strings on the field and does not export a name for them, because nothing in the UI switches on a status in more than one place.

The one field that is drift

In the renderer's copy of RecipeStep there is a field that exists nowhere else in the repository:

export interface RecipeStep {
  id: string;
  action: string;
  binding?: BindingSource;
  field?: FormFieldSchema;
  recordedValuePreview?: string;
}
Enter fullscreen mode Exit fullscreen mode
$ grep -rn "recordedValuePreview" src/
src/renderer/types.ts:136:  recordedValuePreview?: string;
Enter fullscreen mode Exit fullscreen mode

One hit, in the declaration itself. The main process does not have that field, the route that serves recipes does not add it, and no component reads it. It is an optional property, so every object the server sends still satisfies the type, the compiler is happy, and the value is undefined forever.

This is the failure mode of hand-copied types, and it is worth being precise about what the failure actually is. Nothing is broken. No user sees anything wrong. What exists is a lie in the type that a future reader will believe: anybody writing a step list in the UI would reasonably render step.recordedValuePreview, ship it, and find a blank space where a value should be, with no error anywhere to explain it.

Drift in this direction is invisible. Drift in the other direction, a field the renderer needs that the main process stops sending, shows up as undefined in the UI at runtime and not at build time, which is the same class of bug with a user attached to it.

Nothing checks any of this

$ grep -rn "src/types\|\.\./\.\./types" src/renderer | wc -l
0
Enter fullscreen mode Exit fullscreen mode

No type crosses the boundary as an import in either direction, and there is no test that compares the two files. The website that sells the app has 140 tests in five files; the app's own checks are three scripts you run by hand, and none of them is about this.

Part of the reason is structural. The two halves compile under different tsconfigs, one commonjs for Node and one bundler for Vite, and src/types.ts sits in the main process's tree with src/renderer explicitly excluded. Sharing the file means deciding which module system owns it.

Except once, and the exception is the fix

There is exactly one import that crosses the boundary in the whole app, and it is not a type:

// src/renderer/components/SitesList.tsx
import { MAX_SEARCHES } from "../../limits";
Enter fullscreen mode Exit fullscreen mode

src/limits.ts is sixteen lines long and exports one number, and its header explains itself:

/**
 * Hard limits shared by the main process and the renderer.
 *
 * Kept in its own module (no imports) so the renderer bundle can pull the same
 * numbers the server enforces, instead of drifting from a hardcoded copy.
 */
export const MAX_SEARCHES = 15;
Enter fullscreen mode Exit fullscreen mode

A module with no imports of its own is safe for both toolchains, because there is nothing for either module system to resolve. That is the whole trick, and it was already applied to the number the app enforces when you add a sixteenth search.

The same pattern would hold the nine identical shapes: a src/wire.ts with no imports, declaring exactly the shapes that cross the socket, imported by both halves. The main process would keep its 16 private types, the renderer would keep its 6, and the 9 that are currently two copies plus five comments would become one copy and no comments.

What I would not move is the three that differ. A narrower view of a shape on the consumer side is a feature, and the moment the UI imports the full RecipeStep it can reach for fields it has no business rendering. Those three stay hand-written, and recordedValuePreview gets deleted today, because a field nothing writes is worse than no field at all.

The app itself is on the download page if you want to see which of these shapes end up on screen, and the help page describes the saved searches that Site and Config are a type for.

Top comments (0)