-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureFix AvailableA PR has been opened for this issueA PR has been opened for this issueSuggestionAn idea for TypeScriptAn 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.
Inlesco, moneydance, kamilzki, m3h0w, gooxpf and 206 moreInlesco, moneydance, m3h0w, israelKusayev, pacop and 24 morenxpatterns, SheppardX, karrtikr, djobbo, Nonudian and 19 morenxpatterns, SheppardX, karrtikr, djobbo, NishantGurung93 and 16 morenxpatterns, SheppardX, elite0226, karrtikr, djobbo and 12 more
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureFix AvailableA PR has been opened for this issueA PR has been opened for this issueSuggestionAn idea for TypeScriptAn idea for TypeScript