fix: export HASHES as a string union instead of a const enum#113
Merged
Conversation
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
approved these changes
Jun 27, 2026
erikian
reviewed
Jun 28, 2026
| sha1 = 'sha1', | ||
| sha256 = 'sha256', | ||
| } | ||
| export type HASHES = 'sha1' | 'sha256'; |
Member
There was a problem hiding this comment.
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_ENUMPer 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
erikian
approved these changes
Jun 28, 2026
|
🎉 This PR is included in version 2.0.4 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by Samuel Attard · Slack thread
Closes #53
Before / After
Before:
HASHESis declared as aconst enuminsrc/types.ts. TypeScript erasesconst enums at compile time, so they produce no runtime value. The enum is also referenced by the publichashes?: 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-errorworkarounds or casts.After: the package root exports a public
HASHEStype 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 HASHESis kept as-is (no churn to internal references), and the public type is derived from it insrc/index.ts:Because the enum's key names are
sha1/sha256,keyof typeof HASHES_ENUMresolves to exactly'sha1' | 'sha256'. The only changed file issrc/index.ts;src/types.tsandsrc/sign-with-signtool.tsare unchanged frommain.This is non-breaking: a
const enumhas 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), andnpm 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 generateddist/index.d.ts, confirming the published type resolves to'sha1' | 'sha256'.