Enums Flashcards
What is an enum
?
An enum
(short for enumeration) is a special type of value that allows for a collection of related values with meaningful names. Enums are used to define a set of named constants that can represent a range of values, making the code more readable and expressive.
Enums in can be numeric or string-based.
// numeric enum Direction { Up = 1, Down, Left, Right } // string-based enum Color { Red = "RED", Green = "GREEN", Blue = "BLUE" }
“Handbook - Enums” (typescriptlang.org). Retrieved June 19, 2023.
How do you define an enum
?
An enum
can be defined using the enum keyword.
enum Direction { Up = 1, Down, Left, Right, }
“Handbook - Enums” (typescriptlang.org). Retrieved June 19, 2023.
What is a numeric enum
?
A numeric enum
in is a type of enum where the members have numeric values. It is used to define a set of named numeric constants, making the code more readable and expressive.
“Numeric enums” (typescriptlang.org). Retrieved June 19, 2023.
How are values assigned to members in a numeric enum
by default?
By default, the first member of a numeric enum
is assigned the value of 0
, and each subsequent member is incremented by 1.
Example:
enum Days { Sunday, // 0 Monday, // 1 Tuesday, // 2 ... }
“Numeric enums” (typescriptlang.org). Retrieved June 19, 2023.
How can you assign custom values to members in a numeric enum
?
You can assign custom numeric values to members of a numeric enum
by explicitly setting their values. If auto-incrementing continues from the last custom value.
Example with custom values:
enum ErrorCode { NotFound = 404, InternalServerError = 500 }
Example with mixed auto-incremented and custom values:
enum Mixed { A = 1, B, // 2 C = 10, D // 11 }
“Numeric enums” (typescriptlang.org). Retrieved June 19, 2023.
What is a string-based enum
?
A string-based enum
is a type of enum where the members have string values. It is used to define a set of named string constants, making the code more readable and expressive.
“String enums” (typescriptlang.org). Retrieved June 19, 2023.
How are values assigned to members in a string-based enum
?
In a string-based enum
, you must explicitly assign string values to each member. Unlike numeric enums, string enums do not have auto-incrementing behavior.
Example:
enum Color { Red = "RED", Green = "GREEN", Blue = "BLUE" }
“String enums” (typescriptlang.org). Retrieved June 19, 2023.
What are constant enum members?
Constant enum members are members of an enum whose values are known at compile time. They can be literal values or composed of other constant enum members.
TypeScript inlines the value of constant enums at the usage sites, which means the compiled JavaScript code doesn’t include the enum object.
“Computed and constant members” (typescriptlang.org). Retrieved June 20, 2023.
What are computed enum
members?
Computed enum members are members of an enum whose values are computed at runtime. They are usually based on expressions or functions.
Unlike constant enum members, computed enum members require the enum object to be included in the compiled JavaScript.
“Computed and constant members” (typescriptlang.org). Retrieved June 20, 2023.
How do you define a constant enum
member?
Constant enum members are those initialized with a constant enum expression. A constant enum expression is a subset of TypeScript expressions that can be fully evaluated at compile time. An expression is a constant enum expression if it is:
- a literal enum expression (basically a string literal or a numeric literal)
- a reference to previously defined constant enum member (which can originate from a different
enum
) - a parenthesized constant
enum
expression - one of the
+
,-
,~
unary operators applied to constant enum expression -
+
,-
,*
,/
,%
,<<
,>>
,>>>
,&
,|
,^
binary operators with constant enum expressions as operands
Note: It is a compile time error for constant enum expressions to be evaluated to NaN
or Infinity
.
Example:
enum Days { Sunday = 0, Monday = 1, Tuesday = 2, // ... }
“Computed and constant members” (typescriptlang.org). Retrieved June 20, 2023.
How do you define a computed enum
member?
Computed enum members are defined by assigning a non-constant expression to them.
Example:
enum FileAccess { // constant members None, Read = 1 << 1, Write = 1 << 2, ReadWrite = Read | Write, // computed member G = "123".length, }
“Computed and constant members” (typescriptlang.org). Retrieved June 20, 2023.
What happens when you mix constant and computed members in an enum?
When you mix constant and computed members in an enum, the computed members must come after the constant members. Any computed enum member that doesn’t have an initializer is considered to be constant if all preceding enum members are constant.
Example:
enum FileAccess { // constant members None, Read = 1 << 1, Write = 1 << 2, ReadWrite = Read | Write, // computed member G = "123".length, }
“Computed and constant members” (typescriptlang.org). Retrieved June 20, 2023.
What is a const enum
?
A const enum is a special kind of enum in TypeScript. By adding the const
keyword, TypeScript will inline the values at the usage sites. This has the benefit of reducing runtime overhead as the enum object is not included in the compiled JavaScript. Example:
const enum Directions { Up, Down, Left, Right }
“const enums” (typescriptlang.org). Retrieved June 20, 2023.
Can you access the enum object at runtime for const enums
?
No, const enums are completely removed during compilation and only the values are inlined where they are used. Therefore, you cannot access the enum object at runtime.
“const enums” (typescriptlang.org). Retrieved June 20, 2023.
How is the first member an enum
initialized by default if it has no initializer?
The first member of a TypeScript enum
is assigned the value 0
by default if it has no initializer.
For example:
enum E { X, }
In this example, E.X
is equal to 0
.
“Computed and constant members” (typescriptlang.org). Retrieved June 20, 2023.