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
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).
@ahejlsberg so how to properly compare enum values? Such change broke our existing code using TS < 2.x.
@psulek You shouldn't have to change anything. If something broke in your code, the compiler is likely pointing out something suspicious. Can you send a repro of what you're seeing?
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
I encountered the similar error, and have no idea on how I can make the following work:
export enum SortOrder {
NONE,
ASC,
DESC
}
....
public sort(array: any[], order: SortOrder): any[] {
if (order && order !== SortOrder.NONE) {
// do nothing
} else {
// sort
}
Typescript gives me an error on the if condition as follows:
Operator '!==' cannot be applied to types 'SortOrder.ASC | SortOrder.DESC' and 'SortOrder.NONE'.
Typescript version: 2.0.2
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
Ah cheers @vladima
@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 !===
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.)
@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()
In my case I used a switch instead of if/else, it worked without any error.
I am facing the same issue when I compare enum value to a string
export enum Widget {
ICONWIDGET = "IconWidget",
};
getWidgetComponent(componentName:string) {
if(componentName === Widget.ICONWIDGET){
return IconWidgetComponent;
}
}
i am getting runtime error '===' cannot be applied to types 'string' and 'Widget'. how can I resolve this?
I Resolved by my issue by upgrade the typescript version from 2.1.4 to 2.4.1
https://stackoverflow.com/questions/45068473/cannot-be-applied-to-types-string-and-enum-string-value
Maybe this helps someone; In my case I was accidentally having the same comparison in a *nfIg.
E.g.:
*ngIf="a != b && a != b"
I believe the following code is valid, but TS tells me Operator '===' cannot be applied to types 'MyEnum.Constant1' and 'MyEnum.Constant2:
enum MyEnum { C1, C2 };
function test(features: string[]): void {
let myValue = MyEnum.C1;
features.forEach(feature => {
if (feature === 'X') {
myValue = MyEnum.C2;
}
});
if (myValue === MyEnum.C2) { // ERRORS HERE
console.log("Got C2");
}
}
If I instead change test() to the following, the error goes away:
function test(features: string[]): void {
let myValue = MyEnum.C1;
for (let i = 0; i < features.length; ++i) {
if (features[i] === 'X') {
myValue = MyEnum.C2;
}
}
if (myValue === MyEnum.C2) { // NO ERROR
console.log("Got C2");
}
}
Is this desired behavior?
Same issue here. Any ideas?!
Same issue here, typescript version 2.6
Severity Code Description Project File Line Suppression State
Error TS2365 (TS) Operator '===' cannot be applied to types 'ChMActivityStatus.PendingChange' and 'ChMActivityStatus.SubmittedForApproval'.
@mkoutroupis Your code 100% has a bug in it. If status was SubmittedForApproval, then it is certainly !== PendingChange and you would not be testing the condition of the else. The only possible value for status in the else case is PendingChange
@RyanCavanaugh Yes you are right but still the error is the same whether it's an if or else if statement.
Please post a usable repro
@RyanCavanaugh you can download the sample application from here: https://github.com/mkoutroupis/TestTypescript
@mkoutroupis again see #9998
Most helpful comment
enum values starts with
0so in your codeSortOrder.NONEwill have numeric value0. First part of the checkorder && order !== SortOrderNonealready rules out falsy values so second partorder !== SortOrder.NONEeffectively becomes useless since it is always true