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
Enable constants as computed values for string enums #40793
Please consider enabling to use constant variables as values for string enums. The following code snippet fails compilation with TS18033 ('Only numeric enums can have computed members, but this expression has type 'string'. If you do not need exhaustiveness checks, consider using an object literal instead.')
constVALUE: string='ENUM_VALUE';enumMyEnum{KEY=VALUE,// <-- Fails with TS18033}console.log(MyEnum.KEY);
Use string interpolation (templated strings) when defining the value of a string enum to be able to reuse pre-defined constants as part of the value. (E.g. namespace prefixes, extension postfixes)
Reacted by Eric Feminella, Borys Szygenda, idiosync, Sam Craig, Dustin J Silk, Vincent Lynch, Dezone, Abhijeet Singh, Christophe Duchesne-Ashworth, vedro-compota and 25 more
Reacted by Enterprise Software Architecture, Diptesh and Tomáš Šesták
I'm also curious about why computed enums are disabled for strings. Being able to use templated strings would be great. Looks like there was a similar issue here #20440.
Reacted by Enterprise Software Architecture and Karen Dolan
Reacted by Sean Templeton, Sean MacKay, Unless.nz, Patrick Ivers, Abhijeet Singh, Dmitry Burlakov, Hsi Shu Marson Mao, Joe, Lenard Gaida, Hans Fuchs and 17 more
Following the namespace reasoning, highly useful for developing browser extensions or other scripts where we don't want to collision with pre-existing scripts, such as postMessage(<this value>, '*')
Reacted by Victor Hugo, Christopher Helmbold, MufeedaFiroz1, Kubatov Susa, salvadoruriel, Jordan Christiansen, Sydney-o9, aleksgrunwald, Mario Mejia, dengshenkk and 14 more
Additionally symbols should be allowed to be used as enum values.
enumMyType{foo=Symbol("foo"),// Error: ts(18033) - Only numeric enums can have computed members, but this expression has type 'symbol'. If you do not need exhaustiveness checks, consider using an object literal instead.}
Reacted by Victor Hugo, Enterprise Software Architecture, Christopher Helmbold, Kubatov Susa, salvadoruriel, aleksgrunwald, Dmitry Kiselyov, 文宇祥, gonlyk, Supreet Takkar and 2 more
Reacted by Alik Oliveira Rodrigues, Abhijeet Singh, Anouarito, jakubdanicki, Christopher Helmbold, Kubatov Susa, salvadoruriel, Jordan Christiansen, Eugene Orekhov, aleksgrunwald and 5 more
Facing the same issue. I would love to have computed values for strings so I can assign react components to enums.
Reacted by Christopher Helmbold, F-bh, Jordan Christiansen, Eugene Orekhov, Mario Mejia, Enterprise Software Architecture, Dmitry Kiselyov and adrianpangmsft
Reacted by Emanuel Lindström, Enterprise Software Architecture, Dmitry Kiselyov, ITpandaFFM, Pietro Nadalini, Andrei Cristea, Sahar Shemesh, Nacho Sáenz de Tejada, SlikeDollar, Davit Voskanyan and 5 more
Reacted by Abhijeet Singh, F-bh, salvadoruriel, Jordan Wood, Petteri Pesonen, Eugene Fedoryachenko, Jordan Christiansen, Eugene Orekhov, Mario Mejia, Enterprise Software Architecture and 2 more
Reacted by Simon Chan, trent, std4453 and Scott Finlayson
Reacted by Abhijeet Singh, Jack McNicol, Jordan Wood, Eugene Fedoryachenko, David Álvarez, Jordan Christiansen, Mario Mejia, Enterprise Software Architecture and Dmitry Kiselyov
Reacted by Gabriel Azevedo, Emil Dudnyk, Hanzhe Li, Adam Krupke, Anna Koshyna, aminta, Tyler Burkhardt, Arthur Vergaças, Aman Sharma, Tomasz Przysiwek and 3 more
Ryan Cavanaugh (@RyanCavanaugh) I think people (myself included) are still seeing this warning after installing TS >5.0
I'd expect this error to not appear if the feature was implemented?
Since const haha = null || "haha" is a constant expression evaluating in typescript as "haha" (not as a string) shouldn't the above code be valid? Or at least be more clear?
I personally think using hard-coded constant expressions (with operators in them) is a bit of a code stink. But I am curious as to how typescript evaluates what is a const expression and what isn't. Are we explicitly not allowed use conditional operators in the declaration of enums even when the expression iteself can be evaluated statically?
Side Note: I've seen a couple examples of people trying to do the same with ternary expressions, but the key difference being that typescript still evaluates the following
constfoo=true ? 'x' : 'y';
as foo: 'x'|'y' despite that it could technically be evaluated as a const expression foo: 'x'
So for anyone curious, ternary expressions still won't work but at least it makes sense why.
Activity
Inlesco commentedon Oct 11, 2020
This would be a perfectly valid feature to implement since computed number values are already allowed.
Is there any reason why computed string values shouldn't be allowed for enums? Perhaps I'm not aware of something tied to the bigger picture?
Ryan Cavanaugh (@RyanCavanaugh) can anybody from the TS team comment on my assumptions above? Thanks a lot.
moneydance commentedon Nov 12, 2020
I'm also curious about why computed enums are disabled for strings. Being able to use templated strings would be great. Looks like there was a similar issue here #20440.
fenduru commentedon Apr 16, 2021
This would be handy when when writing a React component with a prop that controls a css class. Right now I'm doing the following
But it would be both cleaner, and also safer (i.e. prevent accidentally indexing into a non-existing value in the classes object) to do this directly:
tomasdev commentedon May 1, 2021
Following the namespace reasoning, highly useful for developing browser extensions or other scripts where we don't want to collision with pre-existing scripts, such as
postMessage(<this value>, '*')ahnpnl commentedon Jul 6, 2021
I also need this
RebeccaStevens commentedon Nov 19, 2021
Additionally symbols should be allowed to be used as enum values.
Thorugoh commentedon Nov 19, 2021
I'm with the same problem :/
nxpatterns commentedon Nov 29, 2021
please enable, thank you!
daltonrussell commentedon Dec 20, 2021
Facing the same issue. I would love to have computed values for strings so I can assign react components to enums.
SheppardX commentedon Jan 10, 2022
PUSH!! i am currently facing this issue too.
have an ENUM with endpoints and want to reuse pre-defined parts of that
as an example
zheoreh commentedon Jan 19, 2022
+1
estefafdez commentedon Feb 14, 2022
I would love to have this on TS, thanks!
JordanDC2 commentedon Mar 9, 2022
This would be a great feature, currently running into this same issue
37 remaining items
vfa-locltb commentedon Dec 1, 2022
+1
RyanCavanaugh commentedon Dec 13, 2022
Why are people +1ing an implemented feature?
handletastic commentedon Dec 13, 2022
Probably because people don't yet know of it? 🤷🏻♂️ I mean it's been implemented like a month ago only.
youthug commentedon Mar 14, 2023
Hey guys, how to fix this error?
got this in playground (TS version: the latest Nightly):
and this in IntelliJ IDEA (TS version: 5.0.1-rc)
dfl-zeke commentedon Mar 16, 2023
It's been a 4-5 months and still not included into latest release, wym.
RyanCavanaugh commentedon Mar 17, 2023
5.0 came out yesterday and includes this feature.
afarah1 commentedon Jun 2, 2023
I have a use case like this which still yields TS18033 on 5.0.4:
dburgos92 commentedon Jul 6, 2023
On 5.0.4 with angular this doesn't work:
export enum Section{ ASIGN = GlobalConstants.ASIGN DISTRIBUTION = "Distribucion" }nick4fake commentedon Jul 26, 2023
Ryan Cavanaugh (@RyanCavanaugh) this is still an issue even with TS 5.1.6:
hanzheliva commentedon Aug 3, 2023
Ryan Cavanaugh (@RyanCavanaugh) Could you please check what the cause of this problem is?
RyanCavanaugh commentedon Aug 3, 2023
The error message means what it says.
AlexKDawson commentedon Aug 7, 2023
Ryan Cavanaugh (@RyanCavanaugh) I think people (myself included) are still seeing this warning after installing TS >5.0I'd expect this error to not appear if the feature was implemented?
I felt I should clarify... but I have the same question as Hanzhe Li (@hanzheliva) above.
Since
const haha = null || "haha"is a constant expression evaluating in typescript as"haha"(not as a string) shouldn't the above code be valid? Or at least be more clear?I personally think using hard-coded constant expressions (with operators in them) is a bit of a code stink. But I am curious as to how typescript evaluates what is a const expression and what isn't. Are we explicitly not allowed use conditional operators in the declaration of enums even when the expression iteself can be evaluated statically?
AlexKDawson commentedon Aug 8, 2023
Side Note: I've seen a couple examples of people trying to do the same with ternary expressions, but the key difference being that typescript still evaluates the following
as
foo: 'x'|'y'despite that it could technically be evaluated as a const expressionfoo: 'x'So for anyone curious, ternary expressions still won't work but at least it makes sense why.
mgol commentedon Oct 4, 2023
Issues with using one string enum in another seem like TS bugs; I reported a separate issue for this:
#55978
Please upvote if you hit this, too.
Flagged code, for reference:
grigorii-lutkov commentedon Jan 28, 2025
Hmm.... this should work, right?
TS version:
5.6.2I've created an issue #61064