You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expected behavior:
An exception/error that 2 is not one of the enum option
and that i should use the enum and not just a number value. Which in the programming world
is commonly known as Magic number
Enum numbers are by design meant to be not actually enums and string enums are by design actual enums?
I would just expect number enums to act the same as string enums
Hope you can help me out, it's giving different type restrictions depending on if the Enum is a number or string.
If many people feel the same then it's good to discuss about it
I think you would agree with me that the restrictions for string enums should also be reflected with number / default enums and will help the developer gain a more strictly typed environment
Reacted by Kip Robinson, 流浪大法师 and Marcin Rogalski
I don't like it, either. It is what it is, though.
But, yes, number and string enums have different behavior. Also, if you look at the JS emit for number and string enums, you'll see that they're also different.
It's worth noting that JS has no actual enum type. It's a TS concept. Just like namespace.
Activity
AnyhowStep commentedon Feb 12, 2020
Number enums are not really enums at all. It's "by design", even if many people don't agree.
They were originally meant to just be useful aliases for bitmasks.
[Edit]
See: #32690
ismail-codinglab commentedon Feb 12, 2020
Enum numbers are by design meant to be not actually enums and string enums are by design actual enums?
I would just expect number enums to act the same as string enums
Hope you can help me out, it's giving different type restrictions depending on if the Enum is a number or string.
If many people feel the same then it's good to discuss about it
I think you would agree with me that the restrictions for string enums should also be reflected with number / default enums and will help the developer gain a more strictly typed environment
AnyhowStep commentedon Feb 12, 2020
I don't like it, either. It is what it is, though.
But, yes, number and string enums have different behavior. Also, if you look at the JS emit for number and string enums, you'll see that they're also different.
It's worth noting that JS has no actual
enumtype. It's a TS concept. Just likenamespace.I just tell people to not use enums nowadays.
Just do something like,
Or,
It does make the emit uglier, though. In places, you'll see a union type, instead of
MyNumberEnumtypescript-bot commentedon Feb 15, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.