0

Is it possible to give different option to an enum value like that:

enum Clubs {
    ClubA = 100 | 102,
    ClubB = 50,
    ClubC = 71 | 72 | 73
}

When I try to compare some variable that I have to the ClubA type it only shows me the 102 option I doesn't show me the 100 option at all.

3
  • What's your purpose in trying to have multiple values for an enum variant (which won't work, as each variant should have a unique value)? Commented Jan 10, 2021 at 12:40
  • @Aplet123 I just assumed that since I can do something like that let a:number | undefined | someInteface; It will also work with enums... Commented Jan 10, 2021 at 13:13
  • @Keselme that are two completely different things: In the enum you assign a value (with the assignment operator =) whereas here you declare a type of a variable Commented Jan 10, 2021 at 13:16

2 Answers 2

2

No. I think you'll find the code you've shown is doing a bitwise 'or' on 100 and 102. The result of that is 102, and it's assigning it to ClubA.

The reason you can't have alternative values is that TypeScript transpiles the enum into a JavaScript array with a reverse mapping. For example, this maps Clubs['ClubB']=50 in your example AND Clubs[50]='ClubB'. I can't see a way of doing this so ClubB could have alternative values. The transpiled code is below:

var Clubs;
(function (Clubs) {
    Clubs[Clubs["ClubA"] = 102] = "ClubA";
    Clubs[Clubs["ClubB"] = 50] = "ClubB";
    Clubs[Clubs["ClubC"] = 79] = "ClubC";
})(Clubs || (Clubs = {}));
Sign up to request clarification or add additional context in comments.

2 Comments

100 | 102 is not a logical or but a bitwise or. The logical or operator is ||. And the result of a logical or 100 || 102 would be 100 and not 102. And that's also while happening while transpiling. The expression 100 | 102 is evaluated its result 102 and assigned to ClubA. Same with ClubC: 70 | 71 | 72 == 79, thus ClubC has the value of 79,
@derpirscher You're absolutely right, and I've fixed the answer
1

No it's not possible to have multiple values in an enum. Because when transpiling to JS, an enum is transpiled to an object with properties, and every property has a single value only.

You seeing the value of 102 for Clubs.ClubA is a coincidence. Because 100 | 102 is a bitwise OR of two numbers, which has the result of 102. (100 decimal = 1100100 binary and 102 decimal = 1100110 binary, and the bitwise OR of these binaries is 1100110, which is again 102 in decimal). And this is assigned to be the value of ClubA.

If you look at the value of Clubs.ClubC you will notice, it has a value of 79, and not one of 70, 71 or 72. Just because during transpile, the bitwise OR 70 | 71 | 72 is evaluated and assigned to Clubs.ClubC.

Also consider what it would mean for an enum value to have multiple possible values:

let val : Clubs = Clubs.ClubC;

What would be the value of val? Consider furthermore, you are using that enum as a key in a map

let mymap : Map<Clubs, string> = new Map<Clubs, string>();
mymap.put(70, 'value1');
mymap.put(71, 'value2');
mymap.put(72, 'value3');

let somevalue = mymap[Clubs.ClubC];

What would be the content of somevalue?

Comments

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.