Enums does not support literal typing with computed values · Issue #27976 · microsoft/TypeScript (original) (raw)
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:
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.