简体   繁体   中英

How to assign computed values to enum using typescript

I wanted assign values to enum type. note:text and text1 are actually coming from different file.

const text = "sample text";
const text1 = "Sample text";

const enum textDisplay{
  text = text;
  text1 = text1;
}

that's not how enum work, you can't assign dynamic values to enum, computed values are not permitted in an enum with string valued members.

https://www.typescriptlang.org/docs/handbook/enums.html

The enum member is initialized with a constant enum expression. A constant enum expression is a subset of TypeScript expressions that can be fully evaluated at compile time. An expression is a constant enum expression if it is:

  • a literal enum expression (basically a string literal or a numeric literal)

  • a reference to previously defined constant enum member (which can originate from a different enum)

  • a parenthesized constant enum expression

  • one of the +, -, ~ unary operators applied to constant enum expression +, -, *, /, %, <<, >>, >>>, &, |, ^ binary operators with constant enum expressions as operands

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