-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Rule proposal: Const enum #280
Comments
This would be valuable since Babel doesn’t support |
My take on enums: in a lot of cases const enums can be replaced with literal types. actual enums are useful only if they take advantage of declaration merging otherwise they should be const enum which can be replaced with literal types. |
enums are usually nicer than literals for two reasons:
Example: enum StrFoo {
a = 'a'
}
const y: StrFoo = StrFoo.a;
if (y === 'a') { // is compile-time safe, but hard to refactor
}
enum NumFoo {
a = 1
}
const z: NumFoo = NumFoo.a;
if (z === 1) { // is compile-time safe, but hard to refactor
}
enum Foo {
a
}
const x: Foo = Foo.a;
if (x === 'a') { // is NOT compile-time safe
} If you refactor enum names or adjust the values, then you can very easily create subtle bugs that are type safe enum NumFoo {
- a = 1
+ a = 0
+ b = 1
}
const y: NumFoo = NumFoo.a;
if (y === 1) { // this check is compile-time correct, but the logic is now broken because we've changed the enum values.
} There's a big reason that the flow team is scrambling to add enums to their language - because using string literals and string unions is really, really clunky and causes you to have to write a lot of unnecessary code. |
Never considered refactoring. That's a very good point! 👏🏽 |
I'm working on a rule for this. I call it Default config: {
"enum-style": ["error", "no-const"]
}
|
The rule name sounds good. |
How about this:
|
How about an option object: {
"const": "always" | "never" | "any",
// "memberType": "number" | "string" | "any"
} I'm not sure if this rule disallows enum syntax itself. Do other options disallow the union type of literal types? If no, it's not symmetry. |
I'm leaning towards focus on only const. {
"enum-const-style": ["error", "always" | "never"],
// "enum-member-style": ["error", "number" | "string"],
// "enum-syntax-style": ["error", "enum" | "literal-union"],
} This should be the intention of this issue. |
bringing |
I agree and we should remove |
Update my PR to use Can you please explain what |
Sure. I have thought But we should open an issue to discuss it because I'm not sure if the core team accepts it. |
I'm pretty sure you can refactor variant of union of strings at least in VSCode |
Was attempting to open an issue for I personally don't like After parse though this issue, I'm wondering if the proposed rules here would allow banning |
This issue should be closed as a duplicate of #561 #561 (comment) also explains how to solve this using |
They're very different use cases. That one is about banning enums altogether. This one is about allowing enums, but configuring to enforce always using a const enum, or never using a const enum. |
As I mentioned, the comment in issue #561 (comment) explains exactly how to configure eslint to force always use const enum, or to force always use non-const enum |
Sorry, I was on mobile so I missed that. I'll close both this and the other issue with an FAQ article with examples on using |
The user would be able to configure this rule to either enforce always using
const enum
or always disallow it.Relevant TSLint issue: palantir/tslint#1798
The text was updated successfully, but these errors were encountered: