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

Replace ENUM with companion pattern approach #1077

Closed
lukasaric opened this issue Apr 11, 2024 · 2 comments
Closed

Replace ENUM with companion pattern approach #1077

lukasaric opened this issue Apr 11, 2024 · 2 comments
Assignees
Labels
Type: feature New feature or request

Comments

@lukasaric
Copy link
Contributor

lukasaric commented Apr 11, 2024

Is your feature request related to a problem? Please describe.
Currenlty, TS enum is used across the codebase. I would be good to be replaced with the companion pattern approach to increase the type safety.
To have better understanding, here's a good thread why it would be good to avoid enums in TS
Don't use Enums in Typescript, they are very dangerous

Describe the solution you'd like
BEFORE

export enum NetworkName {
  SN_MAIN = 'SN_MAIN',
  SN_GOERLI = 'SN_GOERLI',
  SN_SEPOLIA = 'SN_SEPOLIA',
}

AFTER

export const NetworkName = {
  SN_MAIN: 'SN_MAIN',
  SN_GOERLI: 'SN_GOERLI',
  SN_SEPOLIA: 'SN_SEPOLIA',
} as const;

// eslint-disable-next-line @typescript-eslint/no-redeclare -- intentionally naming the variable the same as the type
export type NetworkName = (typeof NetworkName)[keyof typeof NetworkName];

Describe alternatives you've considered
Pros:

  • Increased type safety
  • Less prone to mistakes and errors (especially for numerics)
  • Decreased output size and improved performance

Cons:

  • Eslint re-declare rule does not have exception for declaring the same name type and variable. Either, the rule should be disabled or we'll add eslint ignore for these cases which will be annoying

Additional context
Add any other context or screenshots about the feature request here.

@lukasaric lukasaric added the Type: feature New feature or request label Apr 11, 2024
@kateberryd
Copy link

Hi @lukasaric I would like to work on this

@ivpavici
Copy link
Collaborator

@kateberryd sorry, Luka reached out earlier to solve this (when he opened the issue), but I forgot to assign him...
Keep an eye on other issues, there will soon be more!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants