Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
How typescript implements enumerations?

I was wondering about how typescript would compile an enumeration into a javascript code. So I implemented the following example:

enum Contagens {
    UM,
    DOIS,
    TRES
}

And it was compiled into this:

"use strict";
var Contagens;
(function (Contagens) {
    Contagens[Contagens["UM"] = 0] = "UM";
    Contagens[Contagens["DOIS"] = 1] = "DOIS";
    Contagens[Contagens["TRES"] = 2] = "TRES";
})(Contagens || (Contagens = {}));

But, I don't understand how it works... someone could explain this code to me?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. The variable var Contagens; This creates the variable that will hold a reference to the enum.

  2. The argument Contagens || (Contagens = {}) the enum is used if it already exists, and will be set to an empty object, if it doesn't. This allows enums to be extended:

enum Contagens {
    UM,
    DOIS,
    TRES
}

enum Contagens {
    CATRE = 4
}
  1. The function function (Contagens) { takes an argument Contagens that is the value from step #2. In this function it will create the entries on the enum object. It has the same name as the outer variable Contagens, so it shadows that variable. But it's the same value, so that doesn't matter much.

  2. The assignment.

Contagens[Contagens["UM"] = 0] = "UM";

The result of an assignment is the value being assigned.* So Contagens["UM"] = 0 does two things. It sets the key "UM" to the value 0, and it returns 0.

That returned 0 is used then in the a second assignment:

Contagens[0] = "UM";

Now the "UM" property has been assigned 0 and the 0 property has been assigned to "UM". The enum now looks like this:

{ UM: 0, "0": "UM" }

This allows you to lookup a value in an enum by its name, or get its name from its value.

Contagens.UM // 0
Contagens[0] // "UM"

Which is handy!


* The result of an assignment when delcaring a variable is undefined, but assigning a property of an object or assigning to an existing variable will return the assigned value. JS is quirky like that.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!