I have a React application that is using Typescript. Right now I'm running into an issue with const enum. Here's my enum:
export const enum Snack { Apple = 0, Banana = 1, Orange = 2, Other = 3 }
The service I'm trying to match up to isn't returning the value, but the index of the item within the enum. So, for instance, if the user is set to snack on an apple, the service is returning a 0 for that user instead of 'Apple'. Ideally, I'd like to do something like:
var snackIndex = UserSnack.type; // returning 0 in this example var userSnack = Snack[snackIndex]; // would return 'Apple'
When I try something similar I'm getting the following error:
error TS2476: A const enum member can only be accessed using a string literal.
Since the service I'm receiving the data from doesn't return the string, I'm having issues getting this working.
Any help is appreciated.
A const Enum is the same as a normal Enum. Except that no Object is generated at compile time. Instead, the literal values are substituted where the const Enum is used. // Typescript: A const Enum can be defined like a normal Enum (with start value, specifig values, etc.)
Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.
The they are useless at runtime argument This is a false argument for typescript in general, let alone Enums. and agree, if at runtime some code tries to change the values of one of your enums, that would not throw an error and your app could start behaving unexpectedly ( that is why Object.
Because they are constants, the names of an enum type's fields are in uppercase letters. You should use enum types any time you need to represent a fixed set of constants.
Just remove the const
modifier.
const
in an enum means the enum is fully erased during compilation. Const enum members are inlined at use sites. You can can't index it by an arbitrary value. In other words, the following TypeScript code
const enum Snack { Apple = 0, Banana = 1, Orange = 2, Other = 3 } let snacks = [ Snack.Apple, Snack.Banana, Snack.Orange, Snack.Other ];
is compiled to:
let Snacks = [ 0 /* Apple */, 1 /* Banana */, 2 /* Orange */, 3 /* Other */ ];
Compare it with non-const version:
enum Snack { Apple = 0, Banana = 1, Orange = 2, Other = 3 } let Snacks = [ Snack.Apple, Snack.Banana, Snack.Orange, Snack.Other ];
it is compiled to:
var Snack; (function (Snack) { Snack[Snack["Apple"] = 0] = "Apple"; Snack[Snack["Banana"] = 1] = "Banana"; Snack[Snack["Orange"] = 2] = "Orange"; Snack[Snack["Other"] = 3] = "Other"; })(Snack || (Snack = {})); let Snacks = [ Snack.Apple, Snack.Banana, Snack.Orange, Snack.Other ];
Source: const enums @ typescriptlang.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With