Repository navigation
Closed
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureFix AvailableA PR has been opened for this issueSuggestionAn idea for TypeScript
Milestone
Description
Search Terms
'TS18033', 'typescript define enum with constant string value', 'string enum computed constant'
Suggestion
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.')
const VALUE: string = 'ENUM_VALUE';
enum MyEnum {
KEY = VALUE, // <-- Fails with TS18033
}
console.log(MyEnum.KEY);, whereas the following passes:
enum Values {
VALUE = 'ENUM_VALUE',
}
enum MyEnum {
KEY = Values.VALUE,
}
console.info(MyEnum.KEY);Use Cases
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)
Examples
const NAMESPACE: string = 'com.mycompany.myservice';
enum Errors {
INVALID_INPUT_ERROR = `${NAMESPACE}.errors#InvalidInput`,
}Checklist
My suggestion meets these guidelines:
- [ X ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [ X ] This wouldn't change the runtime behavior of existing JavaScript code
- [ X ] This could be implemented without emitting different JS based on the types of the expressions
- [ X ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- [ X ] This feature would agree with the rest of TypeScript's Design Goals.
Reacted by Dezone, Benjamin A Pusey, Kamil Sieradzki, Michał Gacka, GooxPF, Stojan Peshov, Adriano Machado, Krzysztof Proszkiewicz, Paolo Bernasconi, selvindenis97 and 201 more
Reacted by Dezone, Benjamin A Pusey, Michał Gacka, Israel kusayev, pacop, Dmitry Kiselyov, Christophe Duchesne-Ashworth, Memfisrain, Jeffrey Lu, Victor Hugo and 19 more
Reacted by Enterprise Software Architecture, Christopher Helmbold, Kartik Raj, Victor, William CHAZOT, Nishant Gurung, Vova-Herchuk, Listiani, William CHAZOT, wawrzynmc and 14 more
Reacted by Enterprise Software Architecture, Christopher Helmbold, Kartik Raj, Victor, Nishant Gurung, Listiani, William CHAZOT, William CHAZOT, wawrzynmc, aleksgrunwald and 11 more
Reacted by Enterprise Software Architecture, Christopher Helmbold, elite0226, Kartik Raj, Victor, William CHAZOT, William CHAZOT, wawrzynmc, Artem, Julia and 7 more
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureFix AvailableA PR has been opened for this issueSuggestionAn idea for TypeScript
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