Skip to content

[Feature Request] Add Support for const enum in TS #8741

@calebeby

Description

@calebeby

Feature Request

In the docs, it says that:

Does not support const enums because those require type information to compile.

I don't believe that this is true. Babel should be able to transpile const enums into objects, which will achieve the same functionality as typescript, through different transpiled output.

Given this code:

const enum Direction { Left, Right, Down, Up }
console.log(Direction.Down)

tsc would compile this to:

console.log(2)

I propose that Babel compiles the enum to:

const Direction = {
  Left: 0,
  Right: 1,
  Down: 2,
  Up: 3
}
console.log(Direction.Down)

Which functions the same as the tsc output.

A minifier like terser/uglify is able to transpile that output ☝️ to the same as the tsc output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: typescriptoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions