Skip to content

Use const enum values as keys of object literals #14682

@gdh1995

Description

@gdh1995

I want to use values of a const enum to define object literals

** Syntactic **

const enum E {
  Aa = 1, Bb = 2
}
let obj = {
  E.Aa: "action a",
  E.Bb: "action b",
};

Emit

let obj = {
  1: "action a",
  2: "action b",
};

Compatibility
Since JS supports string literals keys, and strings containing . is not a valid id, we may limit the usage of const enums to "EnumTypeName.EnumValueName" to avoid ambiguities. And then,

  • { e_Aa: 1 } is { "e_Aa": 1}
  • { E.Aa: 1 } is { 1: 1}
  • { "E.Aa": 1 } is { "E.Aa": 1}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions