Skip to content

TypeScript integer enum objects can be assigned any integer value #38294

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

Closed
SukeshMarla opened this issue May 1, 2020 · 4 comments
Closed

TypeScript integer enum objects can be assigned any integer value #38294

SukeshMarla opened this issue May 1, 2020 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@SukeshMarla
Copy link

I have enum as follows.

const enum Mode{
Add=1,
Edit=22
}
let currentMode:Mode=222222;
Above code is considered as valid code by compiler.

  • It should throw compile because it is directly assigned integer. It should enforce to assign Value as
    let currentMode:Mode=Mode.Add; //Direct int should not be allowed

  • it should throw compile error because only allowed values should be 1 and 22.

Its Typed Script.

@vassudanagunta
Copy link

I understand why TS enums behave as above (They are not types). But the Typescript documentation says:

The other change is that enum types themselves effectively become a union of each enum member. While we haven’t discussed union types yet, all that you need to know is that with union enums, the type system is able to leverage the fact that it knows the exact set of values that exist in the enum itself. Because of that, TypeScript can catch silly bugs where we might be comparing values incorrectly. For example:

enum E {
    Foo,
    Bar,
}

function f(x: E) {
    if (x !== E.Foo || x !== E.Bar) {
        //             ~~~~~~~~~~~
        // Error! This condition will always return 'true' since the types 'E.Foo' and 'E.Bar' have no overlap.
    }
}
In that example, we first checked whether x was not E.Foo. If that check succeeds, then our || will short-circuit, and the body of the ‘if’ will run. However, if the check didn’t succeed, then x can only be E.Foo, so it doesn’t make sense to see whether it’s equal to E.Bar.

But running the following seems to contradict that.

enum E {
    Foo,
    Bar,
}

function f(x: E) {
    if (x !== E.Foo || x !== E.Bar) {
        // Error! This condition will always return 'true' since the types 'E.Foo' and 'E.Bar' have no overlap.
        console.log(`Is ${x} the sign of the apocalypse?`)
    }
}

let x: E
x = 666
f(x)

@SukeshMarla
Copy link
Author

yes exactly, the whole idea of using enum getting failed here.

In case of string enums its not the case.

const enum Mode{
Add="Add1".
Edit="Edit1"
}
let currentMode:Mode="Add1"; // It will fail
it enforce us to do
let currentMode:Mode=Mode.Add

which seems fair, why such enforcement is not added for integers. It supposed to be.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 5, 2020
@RyanCavanaugh
Copy link
Member

Duplicate #26362 and others; please search before logging new issues when the behavior you're describing has been present since TypeScript first shipped 7 years ago.

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' 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
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants