Skip to content

Rule Proposal 'prefer-enum-initializers' #2268

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

Closed
kaisermann opened this issue Jul 2, 2020 · 1 comment · Fixed by #2326
Closed

Rule Proposal 'prefer-enum-initializers' #2268

kaisermann opened this issue Jul 2, 2020 · 1 comment · Fixed by #2326
Labels
enhancement: new plugin rule New rule request for eslint-plugin good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@kaisermann
Copy link

kaisermann commented Jul 2, 2020

Proposal

{
  "rules": {
    "@typescript-eslint/prefer-enum-initializers ": ["off"]
  }
}

Proposal Result

image

The name and message are just an example.

Actual Result

n/a
image

Additional Info

Enum infers sequential numbers automatically when initializers are omitted:

enum Status {
  Open, // infer 0
  Closed, // infer 1
}

declare const updateStatus: (value: Status) => void

updateStatus(0) // same as updateStatus(Status.Open)

With this approach, if new values are declared without following the existing order and the value of the enum has been saved in an external source, we will have inconsistencies between the saved value and the new representation, example:

enum Status {
  Idle, // new value
  Open,
  Closed,
}

type Resource = { status: Status }

const resource = findResource(id) // previous have { status: Status.Open }

resource.status === Idle // returns true

And removing an option from the enum is not possible, unless it is the last option in the enum, otherwise the order will break as well.

For the my current company's eslint-preset we've implemented a simple version of this rule here. As dealing with enums in typescript can have its trade-offs, I'd like to know if this would be useful to others.

Oh and thanks for the great work with all these packages! They are being extremely useful and effective in guaranteeing the quality of our code 😁 🙌

@kaisermann kaisermann added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 2, 2020
@tiagonapoli
Copy link

tiagonapoli commented Jul 2, 2020

I think this would be a great addition: this enum behavior subtlety can be the source for bugs difficult to track down

@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin good first issue Good for newcomers and removed triage Waiting for team members to take a look labels Jul 2, 2020
@bradzacher bradzacher added the has pr there is a PR raised to close this label Jul 26, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants