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
{{ message }}
Repository navigation
Enums does not support literal typing with computed values #27976
a Should have type of AnimalFlags.EatsFish b Should not throw. Should allow the assignment of AnimalFlags.EndangeredFlyingClawedFishEating as type.
I would expect a computed Enum to behave as a regular enum, in this case, c has type RegularEnum.HasPowers
Actual behavior:
a is typed as AnimalFlags. const b: AnimalFlags.EndangeredFlyingClawedFishEating is throwing Enum type 'AnimalFlags' has members with initializers that are not literals. b should be allowed to be typed as AnimalFlags.EndangeredFlyingClawedFishEating.
I think is the same that one reported last year #18393. It was closed as a Design Limitation, but since there has been some big changes in Typescript maybe this would be worth to be reviewed again.
This is still a design limitation. For mostly historical reasons we have two fundamentally different kinds of enums in the language, numeric and literal. Numeric enums are just like the number type, except that distinct numeric enums are not assignable to each other. Literal enums are quite different: Each enum member has a distinct unit type, and the enum type itself is a union of the enum member types. As described in #9407, we distinguish between the two based on how the members are declared:
When each member of an enum type has either an automatically assigned value, an initializer that specifies a numeric literal, or an initializer that specifies a single identifier naming another enum member, that enum type is considered a union (a.k.a. literal) enum type.
We chose to do it this way such that we didn't have to introduce an explicit modifier to choose between the two kinds. This still holds. The only way I can see us allowing computed constant expressions for literal enum members is to introduce a modifier, e.g. literal enum E { ... }. But I'm not crazy about that idea.
Is there any chance of revisiting this? This would allow for composition of enums. My use case is this, as part of an integration with the HubSpot CRM where booleans are passed as "yes"/"no":
I know it's not super necessary but it's the sort of thing I would expect to "just work", where as currently it gives me an unexpected error which leads me here.
Reacted by Vanley, Ethan Matthews, Onwuka Gideon, Filip Čulig, Richard Waller, BHuber-PlanB, xxxKOTxxx, IhorKulikovskyi, Abhijeet Singh, Camille Drapier and 53 more
enum FileAccess {
// constant members
None,
Read = 1 << 1,
Write = 1 << 2,
ReadWrite = Read | Write,
// computed member
G = "123".length,
}
But the computed doesn't work as the documents described, and throws an error in Vue3 SFC.
TS2535: Enum type has members with initializers that are not literals.
enum FileAccess { // constant members None, Read = 1 << 1, Write = 1 << 2, ReadWrite = Read | Write, // computed member G = "123".length, }
But the computed doesn't work as the documents described, and throws an error in Vue3 SFC. TS2535: Enum type has members with initializers that are not literals.
It's ok that you can define the numeric enum "FileAccess" which has both computed and constant members. However, if you want to use the enum member as a type, the members of the numeric enum must be initialized by literal.
constnum=1;// it‘s okenumUserResponse{// constant member No=0,// computed memberYes=num,// constant member NotSure=1+1}// throws an error : Enum type has members with initializers that are not literalstypeaType=UserResponse.Yes;// throws an error : Enum type has members with initializers that are not literalstypebType=UserResponse.NotSure;// it‘s okenumUserResponse_1{// constant member initialized by literalNo=0,// constant member initialized by literalYes=1,// constant member initialized by literalNotSure=2,}// it's oktypeaType_1=UserResponse_1.Yes;typebType_1=UserResponse_1.NotSure;
In addition, this problum has been resolved in 5.0.
Reacted by Elroy Shilling, Rafael Rodrigues and Peter Hostačný
Activity
ahejlsberg commentedon Oct 19, 2018
This is still a design limitation. For mostly historical reasons we have two fundamentally different kinds of enums in the language, numeric and literal. Numeric enums are just like the
numbertype, except that distinct numeric enums are not assignable to each other. Literal enums are quite different: Each enum member has a distinct unit type, and the enum type itself is a union of the enum member types. As described in #9407, we distinguish between the two based on how the members are declared:We chose to do it this way such that we didn't have to introduce an explicit modifier to choose between the two kinds. This still holds. The only way I can see us allowing computed constant expressions for literal enum members is to introduce a modifier, e.g.
literal enum E { ... }. But I'm not crazy about that idea.lukeramsden commentedon May 18, 2021
Is there any chance of revisiting this? This would allow for composition of enums. My use case is this, as part of an integration with the HubSpot CRM where booleans are passed as "yes"/"no":
I know it's not super necessary but it's the sort of thing I would expect to "just work", where as currently it gives me an unexpected error which leads me here.
clockwiser commentedon Jan 9, 2023
In the typescript documents, it says each enum member has a value associated with it which can be either constant or computed.
https://www.typescriptlang.org/docs/handbook/enums.html
enum FileAccess {
// constant members
None,
Read = 1 << 1,
Write = 1 << 2,
ReadWrite = Read | Write,
// computed member
G = "123".length,
}
But the computed doesn't work as the documents described, and throws an error in Vue3 SFC.
TS2535: Enum type has members with initializers that are not literals.
sisiea commentedon Feb 10, 2023
It's ok that you can define the numeric enum "FileAccess" which has both computed and constant members. However, if you want to use the enum member as a type, the members of the numeric enum must be initialized by literal.
In addition, this problum has been resolved in 5.0.