Skip to content

TypeCheck: any number can be assigned to number-value enums #49967

Closed
@git9am

Description

@git9am

Bug Report

πŸ”Ž Search Terms

enum, number

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about enums

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

enum TypeA {
    A = 0,
    B = 1,
    C = 2,
}

const valueA1: TypeA = 4; // <-- no error
const valueA2: TypeA = '4';

enum TypeB {
    A = '0',
    B = '1',
    C = '2',
}

const valueB1: TypeB = '4';
const valueB2: TypeB = 4;

πŸ™ Actual behavior

The number can be assigned to an enum without errors.

πŸ™‚ Expected behavior

Errors.

Activity

MartinJohns

MartinJohns commented on Jul 20, 2022

@MartinJohns
Contributor

Duplicate of #26362, which provides plenty of reasoning for this behaviour. Used search terms: enum number in:title

typescript-bot

typescript-bot commented on Jul 22, 2022

@typescript-bot
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

        Participants

        @MartinJohns@RyanCavanaugh@typescript-bot@git9am

        Issue actions

          TypeCheck: any number can be assigned to number-value enums Β· Issue #49967 Β· microsoft/TypeScript