Skip to content

Enums does not support literal typing with computed values #27976

Closed
@michaeljota

Description

@michaeljota

TypeScript Version: 3.2.0-dev.201xxxxx

Search Terms:

  • enum flag
  • enum computed literal typing

Code

enum AnimalFlags {
    None           = 0,
    HasClaws       = 1 << 0,
    CanFly         = 1 << 1,
    EatsFish       = 1 << 2,
    Endangered     = 1 << 3,

    EndangeredFlyingClawedFishEating = HasClaws | CanFly | EatsFish | Endangered,
}

enum RegularEnum {
    None,
    HasPowers,
}


const a = AnimalFlags.EatsFish;
const b: AnimalFlags.EndangeredFlyingClawedFishEating = AnimalFlags.EndangeredFlyingClawedFishEating;
const c = RegularEnum.HasPowers;

Expected behavior:

a Should have type of AnimalFlags.EatsFish
b Should not throw. Should allow the assignment of AnimalFlags.EndangeredFlyingClawedFishEating as type.

I would expect a computed Enum to behave as a regular enum, in this case, c has type RegularEnum.HasPowers

Actual behavior:

a is typed as AnimalFlags.
const b: AnimalFlags.EndangeredFlyingClawedFishEating is throwing Enum type 'AnimalFlags' has members with initializers that are not literals.
b should be allowed to be typed as AnimalFlags.EndangeredFlyingClawedFishEating.

Playground Link:

Link

Related Issues:

I think is the same that one reported last year #18393. It was closed as a Design Limitation, but since there has been some big changes in Typescript maybe this would be worth to be reviewed again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedFix AvailableA PR has been opened for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions