Skip to content

Local enum type variable can not be compared. #12254

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
mishengqiang opened this issue Nov 15, 2016 · 1 comment
Closed

Local enum type variable can not be compared. #12254

mishengqiang opened this issue Nov 15, 2016 · 1 comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mishengqiang
Copy link

TypeScript Version: 2.x

Code

enum Mode {A, B}

class C {
    m: Mode = Mode.A;
    test() {
        if (this.m !== Mode.B) {
            this.m = Mode.B
        }
        let n:Mode = Mode.A;
        if (n !== Mode.B){  // error  Operator '!==' cannot be applied to types 'Mode.A' and 'Mode.B'.
            n = Mode.A;
        }
        let l = Mode.A;
        if (l !== Mode.B){  // ok
            l = Mode.A;
        }
        console.log(this.m)
    }
}

function test(){
    let m: Mode = Mode.A;
    if (m !== Mode.B) {   // error  Operator '!==' cannot be applied to types 'Mode.A' and 'Mode.B'.
        m = Mode.B
    }
}

Expected behavior:
Local enum variable with type annotations can not be compared.
Actual behavior:
Local enum variable with type annotations can be compared.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 15, 2016
@RyanCavanaugh
Copy link
Member

The compiler can see your code and it can tell that you're making a comparison that's never true. See #11200

@mhegazy mhegazy closed this as completed Apr 19, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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

3 participants