Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

we can define any number which is not in our enum. #46759

Closed
guoard opened this issue Nov 10, 2021 · 1 comment
Closed

we can define any number which is not in our enum. #46759

guoard opened this issue Nov 10, 2021 · 1 comment

Comments

@guoard
Copy link

guoard commented Nov 10, 2021

Bug Report

we expect not to be able to define a role number which is not in our enum, but we can :|

enum Role {
    ADMIN,
    AUTHOR
}

interface User {
    role: Role
}

const user1: User = {
    role: 1234
}

🙁 Actual behavior

Compiles with out any Error.

🙂 Expected behavior

Type 'number' is not assignable to type 'Role'.

Different behavior with String

but this code gives us correct error message:

Type '"c"' is not assignable to type 'Role'.

enum Role {
    ADMIN = 'a',
    AUTHOR = 'b'
}

interface User {
    role: Role
}

const user1: User = {
    role: 'c'
}
@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 10, 2021

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

What search terms did you use? You skipped that part of the issue template.

@guoard guoard closed this as completed Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants