4

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was going mad on this for the last hour.... Any thoughts on why they changed the transpilation in newer TS? If I get the first JS correctly, it is essentially creating an enum with 4 fields instead of two (the original fields plus the "reversed" fields). Maybe this caused unintended behaviour?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.