简体   繁体   English

从带有字符串的 Typescript 枚举中获取值

[英]Getting values from Typescript enum with strings

I am trying to get the values out of this enum:我试图从这个枚举中获取值:

enum Sizes {
  Tiny = "Tiny",
  VerySmall = "Very Small",
  Small = "Small",
  Medium = "Medium",
  Large = "Large",
  VeryLarge = "Very Large"
}

With the following code as suggested in other StackOverflows, I get the following output:使用其他 StackOverflows 中建议的以下代码,我得到以下输出:

var text=""
for (var size in Sizes) {
    text = text + "\n" + size;
}

console.log(text);

Tiny
VerySmall
Very Small
Small
Medium
Large
VeryLarge
Very Large

I do not want the entries VerySmall and VeryLarge, why are these appearing and how can I get my desired results?我不想要非常小的和非常大的条目,为什么会出现这些条目,我怎样才能得到我想要的结果?

Thanks!谢谢!

It appears the typescript compiler being used is pre-2.4 where they added string value support in enums.看起来正在使用的打字稿编译器是 2.4 之前的,他们在枚举中添加了字符串值支持。 Usually there's a reverse mapping from values to enums and values are generally numbers.通常存在从值到枚举的反向映射,并且值通常是数字。 But if you attempt to use strings prior to 2.4, the compiler wouldn't know what to do about it (and would actually produce errors) but will still generate the source.但是,如果您尝试使用 2.4 之前的字符串,编译器将不知道如何处理它(并且实际上会产生错误),但仍会生成源代码。

Compare 2.4:比较 2.4:

var Sizes;
(function (Sizes) {
    Sizes["Tiny"] = "Tiny";
    Sizes["VerySmall"] = "Very Small";
    Sizes["Small"] = "Small";
    Sizes["Medium"] = "Medium";
    Sizes["Large"] = "Large";
    Sizes["VeryLarge"] = "Very Large";
})(Sizes || (Sizes = {}));

To 2.3:到 2.3:

var Sizes;
(function (Sizes) {
    Sizes[Sizes["Tiny"] = "Tiny"] = "Tiny";
    Sizes[Sizes["VerySmall"] = "Very Small"] = "VerySmall";
    Sizes[Sizes["Small"] = "Small"] = "Small";
    Sizes[Sizes["Medium"] = "Medium"] = "Medium";
    Sizes[Sizes["Large"] = "Large"] = "Large";
    Sizes[Sizes["VeryLarge"] = "Very Large"] = "VeryLarge";
})(Sizes || (Sizes = {}));

And 2.3 without string values:和 2.3 没有字符串值:

var Sizes;
(function (Sizes) {
    Sizes[Sizes["Tiny"] = 0] = "Tiny";
    Sizes[Sizes["VerySmall"] = 1] = "VerySmall";
    Sizes[Sizes["Small"] = 2] = "Small";
    Sizes[Sizes["Medium"] = 3] = "Medium";
    Sizes[Sizes["Large"] = 4] = "Large";
    Sizes[Sizes["VeryLarge"] = 5] = "VeryLarge";
})(Sizes || (Sizes = {}));

If you wanted to force that reverse mapping in 2.4 and up, you could assert the values to any .如果您想在 2.4 及更高版本中强制进行反向映射,您可以将值断言为any

enum Sizes {
  Tiny = <any>"Tiny",
  VerySmall = <any>"Very Small",
  Small = <any>"Small",
  Medium = <any>"Medium",
  Large = <any>"Large",
  VeryLarge = <any>"Very Large"
}

Just call it a feature.只需将其称为功能即可。

In TypeScrit在 TypeScript 中


enum Sizes {
  Tiny = "Tiny",
  VerySmall = "Very Small",
  Small = "Small",
  Medium = "Medium",
  Large = "Large",
  VeryLarge = "Very Large"
}
class Test {
  constructor() {
    let text = '';

    for (const key in Sizes) {
      if (Sizes.hasOwnProperty(key)) {
        text += Sizes[key] + '<br/>';
      }
    }
    console.log(text);

    text = '';
    Object.keys(Sizes).map(key => text += Sizes[key] + '<br/>');
    console.log(text);
  }
}
let t = new Test();

Remember that a for-In should have a if-Statement!记住 for-In 应该有一个 if-Statement!

Both console.log(text) , should output the same string.两个console.log(text) ,都应该输出相同的字符串。

Tiny<br/>Very Small<br/>Small<br/>Medium<br/>Large<br/>Very Large<br/>

If you log Sizes[size] to console after the for-In you will imagine what happens and why you get the values.如果您登录Sizes[size]后安慰了,你会想到什么发生,为什么你得到的值。

You can use Object.keys to get the enum keys and log the values, like this:您可以使用Object.keys获取枚举键并记录值,如下所示:

enum Sizes {
  Tiny = "Tiny",
  VerySmall = "Very Small",
  Small = "Small",
  Medium = "Medium",
  Large = "Large",
  VeryLarge = "Very Large"
}

for (const size of Object.keys(Sizes)) {
    console.log(Sizes[size]);
}

Output:输出:

Tiny
Very Small
Small
Medium
Large
Very Large

Transpiled example:转译示例:

 var Sizes; (function (Sizes) { Sizes["Tiny"] = "Tiny"; Sizes["VerySmall"] = "Very Small"; Sizes["Small"] = "Small"; Sizes["Medium"] = "Medium"; Sizes["Large"] = "Large"; Sizes["VeryLarge"] = "Very Large"; })(Sizes || (Sizes = {})); for (var _i = 0, _a = Object.keys(Sizes); _i < _a.length; _i++) { var size = _a[_i]; console.log(Sizes[size]); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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