#Is the order of enum reverse mapping guaranteed?

5 messages · Page 1 of 1 (latest)

steep hound
#

Can I rely on the ordering shown in the following example:

https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings

That is, when there are two enums with the same value, the latter one "wins."

For context, my project currently has enums like so:

enum Foo {
a = 0,
b,
c,
MIN_VALUE = 0,
MAX_VALUE = 2,
}

This makes it problematic when accessing the values using the reverse mapping, because Foo[Foo.a] would yield "MIN_VALUE," instead of the desired "a." I'd like to try to fix this by reordering it to:

enum Foo {
MIN_VALUE = 0,
MAX_VALUE = 2,
a = 0,
b,
c,
}

But I'm not sure if I can depend on this behaviour. If there are other solutions to this problem I'd love to hear about it too! Thanks!

#

Is the order of enum reverse mapping guaranteed?

violet elk
#

i think it always compiles to the exact same order as specified and normal/reverse mappings happen at the same time
would be a breaking change if it doesnt one day

steep hound
#

Oh perfect, thank you so much!