简体   繁体   English

TypeScript 字符串枚举中的反向映射

[英]Reverse Mapping in TypeScript string enum

According to the documentation , TypeScript string enums are not supposed to do reverse mapping. 根据文档,TypeScript 字符串枚举不应该进行反向映射。 But when I run the following code on jsfiddle it works:但是当我在 jsfiddle 上运行以下代码时,它可以工作:

enum Person {
 firstName = "First Name",
 lastName = "Last Name",
}

document.querySelector("#app").innerHTML = Person["Last Name"];

Demo: https://jsfiddle.net/u73x80e1/演示: https : //jsfiddle.net/u73x80e1/

What am I missing?我错过了什么?

JSFiddle seems to be using an older version of TypeScript. JSFiddle 似乎使用的是旧版本的 TypeScript。 They are generating the following JS:他们正在生成以下 JS:

var Person;
(function (Person) {
    Person[Person["firstName"] = "First Name"] = "firstName";
    Person[Person["lastName"] = "Last Name"] = "lastName";
})(Person || (Person = {}));
document.querySelector("#app").innerHTML = Person["Last Name"];

The same code on TypeScript Playground generates the following with every version you can choose on there: TypeScript Playground上的相同代码为您可以选择的每个版本生成以下代码:

var Person;
(function (Person) {
    Person["firstName"] = "First Name";
    Person["lastName"] = "Last Name";
})(Person || (Person = {}));
document.querySelector("#app").innerHTML = Person["Last Name"];

This appears to be an open issue on their GitHub: https://github.com/jsfiddle/jsfiddle-issues/issues/1079 .这似乎是他们 GitHub 上的一个悬而未决的问题: https : //github.com/jsfiddle/jsfiddle-issues/issues/1079 That thread states that they are using version 1.7.3 .该线程指出他们正在使用版本1.7.3

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

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