Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace enums with string literal types #5429

Merged
merged 1 commit into from
Jan 27, 2025
Merged

Conversation

bitgoAaron
Copy link
Contributor

@bitgoAaron bitgoAaron commented Jan 26, 2025

  • SignatureShareType, CommitmentType, and EncryptedSignerShareType converted to const objects with type exports

Ticket: CE-5160

@bitgoAaron bitgoAaron changed the title refactor: replace enums with string literal types fix: replace enums with string literal types Jan 26, 2025
@bitgoAaron bitgoAaron marked this pull request as ready for review January 26, 2025 17:05
@bitgoAaron bitgoAaron requested review from a team as code owners January 26, 2025 17:05
@kaustubhbitgo
Copy link

If bundle size, performance is the issue, then instead of introducing a breaking change to replace enums with string literals we can opt for any of the following method.

  1. const enum - Erases enum type completely and provides only type definition. If used then tsc replaces it with string literals.
// file.ts
export const enum CommitmentType {
  COMMITMENT = 'commitment',
  DECOMMITMENT = 'decommitment',
}

// example.ts
console.log(CommitmentType.COMMITMENT)

// example.js (after tsc build)
console.log('commitment')
  1. as const - Provides readonly type definitions, less impact on bundle size since it is a direct object mapping instead of function shenanigans that enums do.
// file.ts
export const CommitmentType = {
  COMMITMENT: 'commitment',
  DECOMMITMENT: 'decommitment',
} as const;

// example.ts
console.log(CommitmentType.COMMITMENT) /* same in compiled js with less overhead */

Have you explored any of these methods?

- SignatureShareType, CommitmentType, and
  EncryptedSignerShareType converted to
  const objects with type exports

Ticket: CE-5160
@bitgoAaron
Copy link
Contributor Author

If bundle size, performance is the issue, then instead of introducing a breaking change to replace enums with string literals we can opt for any of the following method.

  1. const enum - Erases enum type completely and provides only type definition. If used then tsc replaces it with string literals.
// file.ts
export const enum CommitmentType {
  COMMITMENT = 'commitment',
  DECOMMITMENT = 'decommitment',
}

// example.ts
console.log(CommitmentType.COMMITMENT)

// example.js (after tsc build)
console.log('commitment')
  1. as const - Provides readonly type definitions, less impact on bundle size since it is a direct object mapping instead of function shenanigans that enums do.
// file.ts
export const CommitmentType = {
  COMMITMENT: 'commitment',
  DECOMMITMENT: 'decommitment',
} as const;

// example.ts
console.log(CommitmentType.COMMITMENT) /* same in compiled js with less overhead */

Have you explored any of these methods?

good call @kaustubhbitgo , went with your suggestion & included a type export which will satisfy our needs tree shaking the UI.

@bitgoAaron bitgoAaron merged commit 8ee3d50 into master Jan 27, 2025
6 checks passed
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.

3 participants