Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string keyword to enum declaration to automatically generate identical key/values for the enum #39755

Closed
5 tasks done
dfoverdx opened this issue Jul 26, 2020 · 2 comments
Closed
5 tasks done
Labels
Duplicate An existing issue was already created

Comments

@dfoverdx
Copy link

dfoverdx commented Jul 26, 2020

Search Terms

  • string enum
  • #feature-request tag (both open and closed)

Suggestion

Many of our enums use identical strings and values. In fact, my team never uses numeric enums (and I do only rarely). It would be nice if there was a shortcut for this behavior.

Use Cases

I'd use this whenever I make an enum with identical keys and string values.

Examples

Current required syntax

enum ThingType {
  Type1 = "Type1",
  Type2 = "Type2",
}

const enum ConstType {
  Type1 = 'Type1',
  Type2 = 'Type2',
}

const type1 = ConstType.Type1;
const type2 = ConstType.Type2;

enum OverrideType {
  Type1 = 'Type1',
  Type2 = 'OverrideString',
  Type3 = 'Type3',
}

JavaScript output

var ThingType;
(function (ThingType) {
    ThingType["Type1"] = "Type1";
    ThingType["Type2"] = "Type2";
})(ThingType || (ThingType = {}));
const type1 = "Type1" /* Type1 */;
const type2 = "Type2" /* Type2 */;
var OverrideType;
(function (OverrideType) {
    OverrideType["Type1"] = "Type1";
    OverrideType["Type2"] = "OverrideString";
    OverrideType["Type3"] = "Type3";
})(OverrideType || (OverrideType = {}));

Desired syntax

string enum ThingType {
  Type1,
  Type2,
}

const string enum ConstType {
  Type1,
  Type2,
}

const type1 = ConstType.Type1;
const type2 = ConstType.Type2;

string enum OverrideType {
  Type1,
  Type2 = 'OverrideString',
  Type3,
}

Expected JavaScript output (identical)

var ThingType;
(function (ThingType) {
    ThingType["Type1"] = "Type1";
    ThingType["Type2"] = "Type2";
})(ThingType || (ThingType = {}));
const type1 = "Type1" /* Type1 */;
const type2 = "Type2" /* Type2 */;
var OverrideType;
(function (OverrideType) {
    OverrideType["Type1"] = "Type1";
    OverrideType["Type2"] = "OverrideString";
    OverrideType["Type3"] = "Type3";
})(OverrideType || (OverrideType = {}));

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
    I'm pretty sure this syntax pattern is still available.
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@IllusionMH
Copy link
Contributor

Looks like duplicate of #16464

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 30, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants