简体   繁体   English

如何在运行时获取 TypeScript 枚举类型的名称?

[英]How to get the name of TypeScript enum type in runtime?

I would like to programmatically get the name of a TypeScript enum.我想以编程方式获取 TypeScript 枚举的名称。

Say I have an enum like this:说我有一个这样的枚举:

enum MyEnum {
  v1 = 'v1',
  v2 = 'v2',
}

I would like to get the name of this enum programmatically.我想以编程方式获取此枚举的名称。 I tried the following:我尝试了以下方法:

const enumType = MyEnum
console.log(enumType.constructor.name) // I expect this to return "MyEnum"

But it displays "Object" instead.但它显示“对象”。

Is there a way to get name of a TypeScript enum programmatically?有没有办法以编程方式获取 TypeScript 枚举的名称?

First, off the type system exists only at compile time.首先,关闭类型系统仅在编译时存在。 When you have当你有

enum MyEnum {
  v1 = 'v1',
  v2 = 'v2',
}

Then TypeScript knows about the enums and ensures that doing MyEnum['v1'] is correct but raising a compile time problem for MyEnum['banana'] .然后TypeScript知道枚举并确保执行MyEnum['v1']是正确的,但会引发MyEnum['banana']的编译时问题。

Once the code is compiled, however, you have pure JavaScript .然而,一旦代码被编译,你就有了纯 JavaScript This is what gets executed at runtime.这是在运行时执行的内容。

Enums get compiled to plain JavaScript objects double binding the keys and values.枚举被编译为普通的 JavaScript 对象,键和值双重绑定。 That way TS ensures the uniqueness of the enum values at runtime.这样 TS 确保运行时枚举值的唯一性。 The example enum given gets turned into:给出的示例枚举变成:

var MyEnum;
(function (MyEnum) {
    MyEnum["v1"] = "v1";
    MyEnum["v2"] = "v2";
})(MyEnum || (MyEnum = {}));

See on TypeScript Playground 请参阅 TypeScript Playground

Once executed you get a MyEnum variable that holds the plain object { v1: "v1", v2: "v2" }执行后,您将获得一个MyEnum变量,该变量包含普通对象{ v1: "v1", v2: "v2" }

I did say that TS will double-bind the keys and values.我确实说过 TS 会双重绑定键和值。 The example above doesn't demonstrate it well, so here is another one:上面的例子没有很好地展示它,所以这里是另一个:

enum ExampleEnum {
  Apple = 1,
  Banana = 2,
}

will be turned into the plain object:将变成普通对象:

{ 
  1: "Apple", 
  2: "Banana", 
  Apple: 1, 
  Banana: 2 
}

See on TypeScript Playground 请参阅 TypeScript Playground

Thus both the keys (Apple and Banana) are unique, as would be the values (1 and 2), since they are all turned into keys of an object and you cannot have duplicate keys.因此,两个键(Apple 和 Banana)都是唯一的,值(1 和 2)也是唯一的,因为它们都变成了对象的键,并且不能有重复的键。 It also allows you do do MyEnum[MyEnum[key]] to fetch what key is.它还允许您执行MyEnum[MyEnum[key]]来获取key

I don't suggest having duplicate values in your enum.我不建议在您的枚举中有重复的值。 Technically possible but you likely shouldn't.技术上可行,但您可能不应该。 you'd have problems if you do MyEnum[MyEnum[key]]如果你做MyEnum[MyEnum[key]]你会遇到问题

At any rate, TypeScript does not encode the enum name as part of the enum, so once you're at runtime, you just have a plain JavaScript variable to work with.无论如何,TypeScript 不会将枚举名称编码为枚举的一部分,因此一旦您处于运行时,您只需使用一个普通的 JavaScript 变量即可。 And you cannot get the name of a variable in JavaScript .并且您无法在 JavaScript 中获取变量的名称 At best, you can do something like:充其量,您可以执行以下操作:

 function printVariableName(input) { console.log("Name:", Object.keys(input)[0]); console.log("Value:", Object.values(input)[0]); } var myVariable = 42; printVariableName({myVariable});

However, this is just cheating a bit - you're passing an object which contains { myVariable: 42 } .然而,这只是在作​​弊——你传递了一个包含{ myVariable: 42 }的对象。 This only works if you already know what the variable is, so it's of little use if you want to get the name indirectly though a different variable:在您已经知道变量是什么时才有效,因此如果您想通过不同的变量间接获取名称,则几乎没有用:

 function printVariableName(input) { console.log("Name:", Object.keys(input)[0]); console.log("Value:", Object.values(input)[0]); } var myVariable = 42; var myOtherVariable = myVariable; printVariableName({myOtherVariable});

So, you you're passing around enums, eg you have a function like:所以,你正在传递枚举,例如你有一个像这样的函数:

function doSomethingWithEnum(someEnum: MyEnum | MyOtherEnum): void {
  //do something with the passed in `someEnum`
}

Then if you call doSomethingWithEnum(MyEnum) then inside the body of the function you cannot de-reference what the passed in variable was.然后如果你调用doSomethingWithEnum(MyEnum)那么在函数体内部你doSomethingWithEnum(MyEnum)引用传入的变量是什么。

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

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