Skip to content

Any number can be assigned to enum member type #22311

Description

Search Terms:
enum assign number

Code

enum X {
  A, B
}

const x: X.A = 5;

const y: X.A = X.B;

Expected behavior:
2 errors: when assigning number literal and wrong enum member.

Actual behavior:
Typescript allows assigning any number to specific member type.

Playground Link:
http://bit.ly/2CZsUrK

Activity

  1. changed the title [-]Any number can be assigning to enum member type[/-] [+]Any number can be assigned to enum member type[/+] on Mar 3, 2018
  2. DanielRosenwasser commented on Mar 3, 2018

    @DanielRosenwasser
    Member

    I was very close to marking this as a duplicate (mostly with #6131 in mind, but also sort of #8020 - see #8020 (comment)).

    The key thing here is that we're working with an individual enum member type, not just an enum type.

  3. rlz commented on Mar 3, 2018

    @rlz
    Author

    Yes, I expected member type is 0 in this case, so I should be able to assign 0 but not 5.

  4. added
    SuggestionAn idea for TypeScript
    and removed
    BugA bug in TypeScript
    on Mar 3, 2018
  5. RyanCavanaugh commented on Apr 16, 2018

    @RyanCavanaugh
    Member

    So we'd allow e.g. const x: X.A = 5 + 0 but not = 5 ?

  6. RyanCavanaugh commented on Aug 7, 2018

    @RyanCavanaugh
    Member

    The key thing here is that we're working with an individual enum member type, not just an enum type.

    The problem here is that X is just an alias for X.A | X.B, so any argument about assignability to X.A applies equally to assignability to X.

    Given the back compat constraints and unlikelihood of finding a bug in userland, keeping the current behavior seems like the best choice of action.

  7. added
    DeclinedThe issue was declined as something which matches the TypeScript vision
    and removed on Aug 7, 2018
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

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions