-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
enum with bigger integer than available is allowed #8020
Comments
We don't distinguish between flag and non-flag enums, so a number that is above any given enum member isn't necessarily invalid. For example enum Flags {
Neat = 1,
Cool = 2,
Great = 4
}
// like saying Neat | Cool | Great
var x: Flags = 7; |
Can I opt-out from that? This is unexpected for me and for my use cases not useful. Or can I use strings as values and reference them like |
There's no opt-out setting. Can you show an example of what you mean about using strings? |
Say the function
However I need to pass
(You need Going back to my original example I probably would like something like
Conclusion Maybe I should rather write a dictionary proposal which works exactly like this?:
compiles to
|
I also think that the flag-behavior of enums would have been nice to have separated into another keyword like However I'm unsure and curious about what you want to achieve here, why don't you just reference the enum value on the ModelType directly like |
Oh, I don't want the send integers directly. I do use the reference like |
@donaldpipowitch So that stuff about strings, how would that improve the language? |
You could reference the string instead of passing it directly. Or what do you mean? |
I mean, this is still about how to work with enums right? Or have this issue changed to be about the general possibility to reference values in string literal types like we do with enums? |
Yeah. I'd definitely like to see an enum alternative to not allow things like that:
That would probably a second issue. However this could both be solved with the same feature, if I could define an |
The issue referenced in OP is by design as noted in #8020 (comment) The rest of the discussion is about non-number based enums, and this is already tracked by #1206. |
maybe, you guys could use:
It will throw the desired errors, and will preserve refactoring capabilities. |
Code
Example
Expected behavior:
Throw error, because
2
shouldn't be available inModelType
.Actual behavior:
No error is thrown.
The text was updated successfully, but these errors were encountered: