I have this in a TypeScript enum:
export enum LMXLockRequestError {
MaxRetries = 'bad_args',
BadArgumentsError = 'bad_args',
}
this doesn't seem to cause a compilation error. It transpiles to this:
var LMXLockRequestError;
(function (LMXLockRequestError) {
LMXLockRequestError["MaxRetries"] = "bad_args";
LMXLockRequestError["BadArgumentsError"] = "bad_args";
})(LMXLockRequestError = exports.LMXLockRequestError || (exports.LMXLockRequestError = {}));
if I were to then use it to do:
if(v === LMXLockRequestError.MaxRetries){
}
if v was 'bad_args' it would match both MaxRetries and BadArgumentsError.
Is this supposed to happen? Or should I file an issue with TypeScript on Github?
To me an enum should have different keys, but maybe not necessarily different values? It would be nice if there was a way to tell the enum that it must have different values.
Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.
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.
In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.
The Python "NameError: name 'Enum' is not defined" occurs when we use the Enum class without importing it first. To solve the error, import the class from the enum module before using it - from enum import Enum . Here is an example of how the error occurs.
regarding the TS ENUM specification:
Enums allow us 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.
There is nothing about that it should to be uniq, so probably that behaivor is okey.
enum/typescript
UPDATE: There is another interesting thing about the ENUM and 'bugs':
Enum value incrementation does not consider previously defined values, nor does the compiler throws an error on duplicate values.
Which means you can end up with potential bugs:
enum Color {Red = 3, Green = 2, Blue};
Color.Red == Color.Blue; //true
To add an actual issue you might encounter wit this (remember that at runtime it's the value that is used) :
enum Toto {
A = "a",
B = "a"
}
const a = Toto.B;
switch (a) {
case Toto.A:
console.log("1");
break;
case Toto.B:
console.log("2");
}
There is no way to enter into case Toto.B case. It would be handy if typescript would NOT allow duplicated names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With