Skip to content

string enums: Infer default value from key #36319

Closed
@KennethKo

Description

@KennethKo

Search Terms

string enum
default value
initializer
infer

Suggestion

Add syntactic sugar to string enum declarations to infer their values from their key names. The overwhelming majority of string enum values are identical or near-identical to their key names, and it's kludgy to list every reserved value twice in its declaration.

enum<string> Foo {
  A,
  B,
  ...,
  Y = 'LegacyOverrideYValue',
  Z,
  LongAndSimilarEnumName,
  LongUnsimilarEnumName,
}

// instead of 

enum Foo {
  A = 'A',
  B = 'B',
  ...,
  Y = 'LegacyOverrideYValue',
  Z = 'Z',
  LongAndSimilarEnumName = 'LongAndSimilarEnumName',
  LongUnsimilarEnumName = 'LongUnSimilarEnumName',
}
// which contains a sneaky bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions