Skip to content

[5.0] Unable to explicitly write type annotation for constant used as enum initializerΒ #52510

@nicolo-ribaudo

Description

@nicolo-ribaudo

Bug Report

πŸ”Ž Search Terms

enum const initializer type

πŸ•— Version & Regression Information

  • I was unable to test this on prior versions because this feature is only supported starting from TS 5.0

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const Const1 = 10;
const enum Enum1 {
    First = Const1,
    Second
}

const Const2: 10 = 10;
const enum Enum2 {
    First = Const2,
    Second
}

const Const3 = 10 as const;
const enum Enum3 {
    First = Const3,
    Second
}

const Const4: number = 10;
const enum Enum4 {
    First = Const4,
    Second
}

πŸ™ Actual behavior

The first enum is valid. For all the other ones, TS reports const enum member initializers must be constant expressions.

πŸ™‚ Expected behavior

At least the second and third enums should be valid, because their initializer is a reference to a const variable with a fixed type. Maybe also the fourth one could work, but I wouldn't be surprised if it doesn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions