简体   繁体   中英

TypeScript: is it possible to dynamically change the values of an enum

is it possible to dynamically change the values of an enum? I have an enum like this

enum MyEnum{
  Error = "Error",
  Warn = "Warnings"
}

I then realized that the values here Error and Warnings have to dynamically generated by react-i18next during the runtime for them to be translated into different languages. So I wonder if there is a way to dynamically plug in the values for the enum. The keys would remain the same all the time.

Well enum in TS are constant and you cannot modify the object once you have declared it. Also, if you are working with internationalisation, using enums are not the best approach, unless you want to translate them every time when a page load or a function gets called.

TS Enum reference: https://www.typescriptlang.org/docs/handbook/enums.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM