Skip to content

Suggestion: Accessing const enum member using variables with preserveConstEnums=true #13753

Closed
@Knagis

Description

@Knagis

Currently it is not possible to access const enum member at runtime by using string or number indexer. This makes sense because by default the declarations for these enums are not emitted in the JS code.

However, we can use preserveConstEnums:true compiler option to both emit the enum declaration in the JS and to have the enum usages still be converted to simple numbers.

As such, it would make sense that with preserveConstEnums:true the enum members should be accessible at runtime as well.

const enum Foo {
    foo1 = 1,
    foo2 = 2 
}

// both lines fail with:
// A const enum member can only be accessed using a string literal.
let fk: "foo1" | "foo2" = Foo[1];
let f = Foo[fk];

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions