Christophe
Christophe

Reputation: 28134

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?

Upvotes: 4

Views: 1252

Answers (1)

Daniel W Strimpel
Daniel W Strimpel

Reputation: 8470

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.

Upvotes: 3

Related Questions