You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
enum with bigger integer than available is allowed #8020
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 ModelType.Gists (instead of using 'foo' | 'bar' and use strings directly).
Say the function test only accepts 'foo' or 'bar' as possible types. I could do it that way:
type Value = 'foo' | 'bar';
function test(value: Value) {
}
test('foo');
However I need to pass 'foo'``directly which can be troublesome for refactoring. I'd rather pass something likeValue.fooas a reference to'foo'` (which also reads nicer in my opinion). Is this possible somehow? The best thing I can come up with is this beast:
type Value = 'foo' | 'bar';
type IValueMap = {
foo: 'foo',
bar: 'bar'
};
const ValueMap: IValueMap = {
foo: 'foo',
bar: 'bar'
};
function test(value: Value) {
}
test(ValueMap.foo);
(You need IValueMap or you'll get Argument of type 'string' is not assignable to to parameter of type '"foo" | "bar"'.)
Going back to my original example I probably would like something like enum, but as a fixed dictionary (which would compile to the same output as my last example with IValueMap):
export dictionary ModelType {
Repositories: 'repos',
Gists: 'gists
};
function test(modelType: ModelType) {
}
test(ModelType.Repositories);
test('repos'); // this would work, too
Conclusion
Maybe I should rather write a dictionary proposal which works exactly like this?:
I also think that the flag-behavior of enums would have been nice to have separated into another keyword like flag since that behavior is really different from the common usage of enums as a a simple set of named constant values.
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 test(ModelType.Gists) instead of sending in wild integers if you want more safety and improving refactoring?
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 test(ModelType.Gists) instead of sending in wild integers if you want more safety and improving refactoring?
Oh, I don't want the send integers directly. I do use the reference like ModelType.Gists. I found that by accident and thought it was a bug.
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?
Or have this issue changed to be about the general possibility to reference values in string literal types like we do with enums?
That would probably a second issue. However this could both be solved with the same feature, if I could define an enum with limited custom values (which could be just 0 and 1 or 'gists' and 'repos').
Activity
RyanCavanaugh commentedon Apr 12, 2016
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
donaldpipowitch commentedon Apr 12, 2016
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
ModelType.Gists(instead of using'foo' | 'bar'and use strings directly).RyanCavanaugh commentedon Apr 12, 2016
There's no opt-out setting. Can you show an example of what you mean about using strings?
donaldpipowitch commentedon Apr 12, 2016
Say the function
testonly accepts'foo'or'bar'as possible types. I could do it that way:However I need to pass
'foo'``directly which can be troublesome for refactoring. I'd rather pass something likeValue.fooas a reference to'foo'` (which also reads nicer in my opinion). Is this possible somehow? The best thing I can come up with is this beast:(You need
IValueMapor you'll getArgument of type 'string' is not assignable to to parameter of type '"foo" | "bar"'.)Going back to my original example I probably would like something like
enum, but as a fixeddictionary(which would compile to the same output as my last example withIValueMap):Conclusion
Maybe I should rather write a dictionary proposal which works exactly like this?:
compiles to
Elephant-Vessel commentedon Apr 12, 2016
I also think that the flag-behavior of enums would have been nice to have separated into another keyword like
flagsince that behavior is really different from the common usage of enums as a a simple set of named constant values.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
test(ModelType.Gists)instead of sending in wild integers if you want more safety and improving refactoring?donaldpipowitch commentedon Apr 12, 2016
Oh, I don't want the send integers directly. I do use the reference like
ModelType.Gists. I found that by accident and thought it was a bug.Elephant-Vessel commentedon Apr 13, 2016
Donald Pipowitch (@donaldpipowitch) So that stuff about strings, how would that improve the language?
donaldpipowitch commentedon Apr 13, 2016
You could reference the string instead of passing it directly. Or what do you mean?
Elephant-Vessel commentedon Apr 13, 2016
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?
donaldpipowitch commentedon Apr 13, 2016
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
enumwith limited custom values (which could be just0and1or'gists'and'repos').mhegazy commentedon Apr 13, 2016
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.
andremarcondesteixeira commentedon Jul 16, 2017
maybe, you guys could use:
It will throw the desired errors, and will preserve refactoring capabilities.