Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are enum Flags in TypeScript?

Tags:

typescript

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.

like image 637
Jaime Rios Avatar asked Sep 07 '16 00:09

Jaime Rios


People also ask

What are enum flags?

The idea of Enum Flags is to take an enumeration variable and allow it hold multiple values. It should be used whenever the enum represents a collection of flags, rather than representing a single value. Such enumeration collections are usually manipulated using bitwise operators.

What are enums in TypeScript?

Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.

Should you use enums in TypeScript?

Enums are just one useful way to organize code in TypeScript. Here are some reasons why enums come in handy: With enums, you can create constants that you can easily relate to, making constants more legible. Developers have the freedom to create memory-efficient custom constants in JavaScript.

Can we change enum values in TypeScript?

An enum is usually selected specifically because it is immutable - i.e. you would never want the values to change as it would make your application unpredictable.


Video Answer


2 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(":(");
}
like image 133
David Sherret Avatar answered Oct 09 '22 02:10

David Sherret


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
like image 42
Patrick Desjardins Avatar answered Oct 09 '22 03:10

Patrick Desjardins