According to the link
Const enums can only use constant enum expressions and unlike regular
enums they are completely removed during compilation. Const enum
members are inlined at use sites. This is possible since const enums
cannot have computed members.
To illustrate consider below code -
enum A {
a = 1,
b = 2
}
namespace N1 {
export const enum A {
a = 1,
b = 2
}
}
console.log(N1.A.a);
console.log(A.a);
which will be transpiled to -
"use strict";
var A;
(function (A) {
A[A["a"] = 1] = "a";
A[A["b"] = 2] = "b";
})(A || (A = {}));
console.log(1);
console.log(A.a);
As you can see -
- There is no reference of
const enum A
in the transpiled code.
- The console output of printing
N1.A.a
converted to literal type
1
.
So at compile time the literal type 1
and A.a
cannot be compared and you have to typecast the const enum
to number like below -
console.log(N1.A.a as number === A.a)
You can further check the literal type comparison in below thread -
Operator '==' cannot be applied to types x and y in Typescript 2