Skip to content

Enum can be assigned out-of-bounds values #30629

Closed

Activity

  1. dragomirtitian commented on Mar 28, 2019

    @dragomirtitian
    Contributor

    I believe this is by design as stated by Daniel Rosenwasser (@DanielRosenwasser) in #26362 to allow for bitwise operations on flag enums:

    The behavior is motivated by bitwise operations. There are times when SomeFlag.Foo | SomeFlag.Bar is intended to produce another SomeFlag. Instead you end up with number, and you don't want to have to cast back to SomeFlag.
    I think if we did TypeScript over again and still had enums, we'd have made a separate construct for bit flags.

  2. jrieken commented on Mar 28, 2019

    @jrieken
    ContributorAuthor

    bitwise operations on flag enums:

    That makes sense but then they should geek out and understand that you cannot produce 12 with 1 and/or 2

  3. dragomirtitian commented on Mar 28, 2019

    @dragomirtitian
    Contributor

    Johannes Rieken (@jrieken) Challenge accepted 😄 :

    let value: Fonum = Fonum.a << Fonum.b << Fonum.a  |  Fonum.a << Fonum.b; // 12

    I think it is difficult to tell if the resulting number is valid or not if we consider flag enums and the whole range of bitwise operations.

  4. jrieken commented on Mar 28, 2019

    @jrieken
    ContributorAuthor

    haha - fair point. There should probably be strict enum that doesn't allow these things...

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions