Skip to content

fix: export HASHES as a string union instead of a const enum#113

Merged
MarshallOfSound merged 2 commits into
mainfrom
hashes-string-union
Jun 28, 2026
Merged

fix: export HASHES as a string union instead of a const enum#113
MarshallOfSound merged 2 commits into
mainfrom
hashes-string-union

Conversation

@claude

@claude claude Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Requested by Samuel Attard · Slack thread

Closes #53

Before / After

Before: HASHES is declared as a const enum in src/types.ts. TypeScript erases const enums at compile time, so they produce no runtime value. The enum is also referenced by the public hashes?: HASHES[] option field, but consumers can't reference the enum's 'sha1'/'sha256' values themselves — so anyone wanting to set that option has to fall back to @ts-expect-error workarounds or casts.

After: the package root exports a public HASHES type that is the 'sha1' | 'sha256' string union. Consumers can now pass the string literals directly (hashes: ['sha256']) with full type-checking and no workarounds.

How

Per review feedback, the internal const enum HASHES is kept as-is (no churn to internal references), and the public type is derived from it in src/index.ts:

import { HASHES as HASHES_ENUM } from './types.js';
export type HASHES = keyof typeof HASHES_ENUM;

Because the enum's key names are sha1/sha256, keyof typeof HASHES_ENUM resolves to exactly 'sha1' | 'sha256'. The only changed file is src/index.ts; src/types.ts and src/sign-with-signtool.ts are unchanged from main.

This is non-breaking: a const enum has no runtime representation, so nothing is removed at runtime, and the public option field shapes are unchanged (consumers gain the ability to reference the string values directly).

npm run build (tsc), npm run lint (oxlint + oxfmt), and npm test (8/8) all pass. Verified via a temp type-assertion (const _ok: HASHES = 'sha256' compiles; 'md5' is rejected, removed before commit) and by type-checking a downstream consumer against the generated dist/index.d.ts, confirming the published type resolves to 'sha1' | 'sha256'.

A `const enum` is erased at compile time, so downstream consumers cannot
reference its values (e.g. `'sha1'`/`'sha256'`). Replace it with an
equivalent `'sha1' | 'sha256'` string union and update internal usages
to the string literals.

Closes #53

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BkvoqcqFJmtAiZoKF9VSwf
@MarshallOfSound MarshallOfSound marked this pull request as ready for review June 27, 2026 21:40
@MarshallOfSound MarshallOfSound requested a review from a team as a code owner June 27, 2026 21:40
Comment thread src/types.ts Outdated
sha1 = 'sha1',
sha256 = 'sha256',
}
export type HASHES = 'sha1' | 'sha256';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: we could keep using an enum internally and just expose a string union generated from that enum; that way, we don't need any refactoring:

// for internal usage
export const enum HASHES {
  sha1 = 'sha1',
  sha256 = 'sha256',
}

// for library consumers - this should probably go in `src/index.ts`
import { HASHES as HASHES_ENUM } from './types'
export type HASHES = keyof typeof HASHES_ENUM

Per review feedback, keep the `const enum HASHES` internal (reverting the
internal string-literal refactor) and instead expose a public derived
string union from the package root:

    export type HASHES = keyof typeof HASHES_ENUM;

Consumers importing `HASHES` from the package root now get the
`'sha1' | 'sha256'` string union, while internal code keeps using the
enum members unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01BkvoqcqFJmtAiZoKF9VSwf
@MarshallOfSound MarshallOfSound merged commit f89187e into main Jun 28, 2026
8 checks passed
@MarshallOfSound MarshallOfSound deleted the hashes-string-union branch June 28, 2026 03:12
@electron-npm-package-publisher

Copy link
Copy Markdown

🎉 This PR is included in version 2.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export HASHES as enum, not const enum

3 participants