I'm learning TypeScript using this ebook as a reference. I've checked the TypeScript Official Documentation but I don't find information about enum flags.
-
1The `FileAccess` example given in the document you reference is an example of this. The very page in the ebook you reference has a section called "enums as flags", which describes what you are looking for, in even more detail than the answer below, including how to add flags using `|=` etc. – Sep 07 '16 at 03:47
4 Answers
They're a way to efficiently store and represent a collection of boolean values.
For example, taking this flags enum:
enum Traits {
None = 0,
Friendly = 1 << 0, // 0001 -- the bitshift is unnecessary, but done for consistency
Mean = 1 << 1, // 0010
Funny = 1 << 2, // 0100
Boring = 1 << 3, // 1000
All = ~(~0 << 4) // 1111
}
Instead of only being able to represent a single value like so:
let traits = Traits.Mean;
We can represent multiple values in a single variable:
let traits = Traits.Mean | Traits.Funny; // (0010 | 0100) === 0110
Then test for them individually:
if ((traits & Traits.Mean) === Traits.Mean) {
console.log(":(");
}

- 101,669
- 28
- 188
- 178
-
7`let traits = Traits.Mean | Traits.Funny` now `traits` variable will have multiple values, how to do the reverse ? I will have `6` how to convert to `Traits.Mean | Traits.Funny` ? – Sreekumar P Mar 06 '18 at 07:49
-
2
-
-
1Mean and Friendly? How's that possible? Jokes aside, good answer, though. – Xwtek Jun 06 '20 at 01:34
-
3@Nieksa yes, the same answer would apply. The only limitation is the size (in bits) of the underlying type, which is a 32 bit integer (and therefore supports 32 flags). This isn't quite the same as in a language like C#, where the underlying type can simply be changed. In js/ts, all bitwise operations behave as if the value size is 32 bits (even though in practice it is 64), so a special library would be needed to handle a 64-bit `enum`. – Daniel Apr 08 '21 at 20:37
The official documentation has this example that I will add some details that are crucial to use enum and flags.
enum FileAccess {
None,
Read = 1 << 1,
Write = 1 << 2,
}
In TypeScript, you can assign a value directly with =
let x:FileAccess = FileAccess.Read;
But this might override previous values. To get around that you can use |=
to append a flag.
x |= FileAccess.Write;
At this point, the variable x
is Read and Write. You can remove a value by using the ampersand and tilde:
x &= ~FileAccess.Read;
Finally, you can compare to see if one of the value is set to the variable. The accepted answer is not right. It should not just use the ampersand symbol but also check with ===
to the desired value. The reason is the ampersand returns a number, not a boolean.
console.log(FileAccess.Write === (x & FileAccess.Write)); // Return true
console.log(FileAccess.Read === (x & FileAccess.Read)); // Return false

- 433
- 5
- 23

- 136,852
- 88
- 292
- 341
You can check this solution, that add some infra, but if you will use it a lot it really nice, the usage goes like that:
let FlaggedExample: IFlaggedEnum = FlaggedEnum.create(Example, 1 << 2); // class definition
let example = new FlaggedExample(3); // Alpha,Beta instance of the class
export module FlaggedEnum {
"use strict";
export interface IFlaggedEnumGenerator {
(_enum: any, _max: number): IFlaggedEnum;
}
export interface IFlaggedEnum {
(val: IFlaggedEnum): void;
(val: number): void;
(val: string): void;
/** array of the individual enum flags that represent the value
*/
toArray(): IFlaggedEnum[];
/** does this instance contain all the flags of the value
*/
contains(val: IFlaggedEnum): boolean;
contains(val: number): boolean;
contains(val: string): boolean;
/** adds the flags to the value and returns a new instance
*/
add(val: IFlaggedEnum): IFlaggedEnum;
add(val: number): IFlaggedEnum;
add(val: string): IFlaggedEnum;
/** removes the flags from the value and returns a new instance
*/
remove(val: IFlaggedEnum): IFlaggedEnum;
remove(val: number): IFlaggedEnum;
remove(val: string): IFlaggedEnum;
/** returns an instance containing all intersecting flags
*/
intersect(val: IFlaggedEnum): IFlaggedEnum;
intersect(val: number): IFlaggedEnum;
intersect(val: string): IFlaggedEnum;
/** does the two instances equal each other
*/
equals(val: IFlaggedEnum): boolean;
equals(val: number): boolean;
equals(val: string): boolean;
}
/** create a class definition for a Flagged Enum
* @method create
* @param _enum {enum} The enum definition being exteded
* @param _max {number} the maximum possible value of the enum being extended
* @returns {IFlaggedEnum} the class definition for the provided enum
*/
export var create: IFlaggedEnumGenerator = function (_enum: any, _max: number): IFlaggedEnum {
var base: any = _enum,
max: number = _max;
var Base: IFlaggedEnum = <any>function (val: any): void {
if (typeof (val) === "string") {
val = base[val];
}
this.value = val + 0;
};
var proto: any = Base.prototype;
proto.valueOf = function (): number { return <number>this.value; };
proto.toString = function (): string {
var list: string[] = [];
for (var i: number = 1; i < max; i = i << 1) {
if ((this.value & i) !== 0) {
list.push(base[i]);
}
}
return list.toString();
};
proto.toArray = function (): IFlaggedEnum[] {
var list: IFlaggedEnum[] = [];
for (var i: number = 1; i < max; i = i << 1) {
if ((this.value & i) !== 0) {
list.push(new Base(i));
}
}
return list;
};
proto.contains = function (val: any): boolean {
if (typeof (val) === "string") {
val = base[val];
}
return (this.value & val) === (val + 0);
};
proto.add = function (val: any): IFlaggedEnum {
if (typeof (val) === "string") {
val = base[val];
}
return new Base(this.value | val);
};
proto.remove = function (val: any): IFlaggedEnum {
if (typeof (val) === "string") {
val = this.base[val];
}
return new Base((this.value ^ val) & this.value);
};
proto.intersect = function (val: any): IFlaggedEnum {
if (typeof (val) === "string") {
val = base[val];
}
var final: number = 0;
for (var i: number = 1; i < max; i = (i << 1)) {
if ((this.value & i) !== 0 && (val & i) !== 0) {
final += i;
}
}
return new Base(final);
};
proto.equals = function (val: any): boolean {
if (typeof (val) === "string") {
val = base[val];
}
return this.value === (val + 0);
};
return Base;
};
}

- 2,788
- 27
- 30
You can just use the bit values do define your enum
enum FileAccess {
None = 0,
Read = 1,
Write = 2,
Active = 4,
Failed = 8
}

- 1,742
- 8
- 18
-
2You're not wrong but this question has been answered 5 years ago and assigning bit-shifted values is the more maintainable solution here. – DustInComp Nov 11 '21 at 11:40