8 - Published at:
a few seconds ago
As you can see, this method bases on an object, holding explicitly allowed values as keys and having the corresponding number saved as a value. Subsequently, you can use these values. On top of that, you can also read and assign those values, they are comparable and have underlying names.
Therefore, I show you the second way. Use a string instead of an enum look-alike object.
But working with strings can bring you drawbacks as well. What is about the overview about allowed or even possible values? This might go under when using this technique. You might think of providing a mix of the first and the second alternative to enums.
We got our enum “itemCategory” set up with strings instead of numbers and can now reassign “weapon.category” with an enum representation.
We kept our overview of defined possibilities of the enum. This isn’t possible when using strings.
Pretty fly for an enum guy
I talked in the first chapter about the possibility to double flag the values of enums. To make that, you use a trick. Define the numbers for the representations of the enum values as a basis for the power of 2. Doing it like this brings the binary combiners to the table.
I advise you to design a default value for your usages. Commonly used for that is the value 0. Possibly we can double assign a value now. For example, a healing potion could also be a healing item, but also for crafting (brewing). To do so, we have to combine the corresponding values.
If you stick to the second alternative, I introduced in chapter 1, then you aren’t able to double flag your properties as I did.
But don’t you also think that this is still like a nifty workaround? Smart, but not as good as the original enum. We will take a look into Typescript now. This is the holy land, where milk and honey run through the rivers.
Enums in TypeScript
TypeScript has enums as types and you can use them easily with the keyword: “enum”.
TypeScript behaves here like C# and starts counting with 0. If you don’t want this, just assign the first enumeration with your favorite number. When numbers aren’t a meaningful representation, then you can also rely on strings as well.
Note: The language allows you to mix up strings and numbers as assignments inside the same enum. But the practicability isn’t quite good on this one. Better stick to one type of assignment per enumeration.
You can realize the combination of enum values in typescript as well. Make sure to have a default value (0).
Taking this example:
We take a deep look into this humongous beast step by step. The first act is the creation of an object called itemCategory in line 1. This will have the value undefined. It will be passed to a function in the next step.
Inside the body of this function, you see four assignments corresponding to our known enum representations “ healing ”, ” crafting ”, “ armor ”, “ weapon ”. But what is that in front of the equal sign? Let's look at the 2nd line in detail.
We observe that this is a creation of the property inside the whole construct. The itemCategory[“crafting”] appends to the newly created object itemCategory (line1 of X10_ConvertedTsEnumIntoJS.js) the property crafting and sets it with itemCategory[“crafting”]=1 to 1.
Because of the returning value of an assignment, the code is instantly creating another property with the name of “1“. This property will get the value “crafting”. If you inspect the line precisely, you will come to the conclusion that this is a dual-assignment. We get the property 1 assigned with “crafting” and get the property “crafting” with the value “1”. There are now two ways to identify the same representation for “crafting”:”1" and “1”:”crafting”. The compiled code results in an object creation like:
This is helpful for displaying the values behind the enum.
There is none expect: “use strict”. The enum vanished entirely. Where is it gone? We make an assignment from our enum:
An inspect the compilation another time:
Like I mentioned, the two directions to get the name by the value and to get the value by the name are gone. It leaves only one of them. The number.
Are there other ways?
Yep, you can use a so-called “Discriminated Union”. This is just a variable of type “type” holding one of many values.
Whatever you want to take, keep aligned for your entire project. Doing mixed usages inside one project will only lead to confusion and constant rethinking. Bugs are also minimized by staying only to one way of doing. To find the right thing for your needs, you have to weigh up the pros and cons of the introduced methods, like always in life!