简体   繁体   中英

Reverse Mapping in TypeScript string enum

According to the documentation , TypeScript string enums are not supposed to do reverse mapping. But when I run the following code on jsfiddle it works:

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

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

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

What am I missing?

JSFiddle seems to be using an older version of TypeScript. They are generating the following 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:

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 . That thread states that they are using version 1.7.3 .

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