TypeScript: Error "Operator '===' cannot be applied to types" with enum type

TypeScript Version: 2.0.3

Code

enum SomeEnum {
    V1,
    V2
}

class SomeClass {
    protected state:SomeEnum

    method() {

        // "Operator '===' cannot be applied to types 'SomeEnum.V1' and 'SomeEnum.V2'." error here
        this.state = SomeEnum.V1
        if (this.state === SomeEnum.V2) {
        }        

        // and the same error here
        if (SomeEnum.V1 === SomeEnum.V2) {
        }

        // but no error here
        let state2 = SomeEnum.V1
        if (state2 === SomeEnum.V2) {
        }
    }
}

Expected behavior: I didn’t expect that error in these cases

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 22 (7 by maintainers)

Most upvoted comments

enum values starts with 0 so in your code SortOrder.NONE will have numeric value 0. First part of the check order && order !== SortOrderNone already rules out falsy values so second part order !== SortOrder.NONE effectively becomes useless since it is always true

The errors are actually the intended behavior. In the first example the compiler knows that this.state has the value SomeEnum.V1 and therefore the comparison will always be false. The second example is just another way of demonstrating that. The third example actually should be an error and is an error in the nightly builds (see #10676 for more detail on what changed here).

In my case I used a switch instead of if/else, it worked without any error.

@effzeh somehow this issue hasn’t linked to #9998 yet, but see the first example there. Short answer: add a type assertion, or change tok to getToken()

I’m getting the same error with the following code (tsc version 2.1.5)

enum Token { eof, ok }

class Parser {
    pos: number = 0;
    tok: Token = this.lex();

    lex(): Token {
        if (this.pos >= 100)
            return Token.eof;
        ++this.pos;
        return Token.ok;
    }

    next(): void {
        this.tok = this.lex();
    }

    parse(): boolean {
        if (this.tok === Token.eof)
            return true;

        // no error...
        // this.tok = this.lex();

        // ERROR in the return statement below
        this.next();

        return this.tok === Token.eof;
    }
}

I just started learning JavaScript/TypeScript, so it’s very likely I’m missing something, but that this code works in one case and not in the other just doesn’t seem right to me. (This is a reduced test-case from a port of a small C++ JSON parser to TypeScript. The generated JS code for the larger parser works as intended.)

@vladima As you are right, such compile error is bug. When enum member is represented by number i should be able to compare number type with === or !===

Maybe it has something to do with generators/yield.

Sample:

export enum State {
    Unknown,
    Initializing,
    InitFailed,
    InitSuccess
}

class Processor {
    engineState: State;

    public * initializeAsync(): Iterable<boolean> {
        this.engineState = State.Initializing;

        try {
            // yield some action
            // yield <any>this.verifyAsync();
            this.engineState = State.InitSuccess;
        } catch (err) {
            this.engineState = State.InitFailed;
        } finally {
            return this.engineState === State.InitSuccess;
        }
    }
}

Save file as “ts-enums.ts” and compile with:

tsc ts-enums.ts -t ES2015

And you got error:

error TS2365: Operator '===' cannot be applied to types 'State.Initializing' and 'State.InitSuccess'

Typescript version: 2.0.3