Reputation: 1628
In my original TS file (foo.ts
), I have this:
export const enum ACCESS_STATE {
master,
readonly,
none,
failed
}
After the Typescript compiler completes, the output (foo.d.ts
) file properly contains this:
const enum ACCESS_STATE {
master = 0,
readonly = 1,
none = 2,
failed = 3,
}
However, the resulting output (foo.js
) file has no reference to ACCESS_STATE
or its values at all. This isn't a problem in foo.js
directly, since the compiler would have properly substituted the ACCESS_STATE.master
and other references to the hard value of 0 (etc).
The problem is that I have other javascript modules that require foo and want to reuse the enum.
Is it possible to have the compiler generate something in the foo.js
that is referenceable and can be used ? Such as:
var ACCESS_STATE = (function () {
function ACCESS_STATE () {
this.master = 0;
this.readonly = 1;
this.none = 2;
this.failed = 3
}
return ACCESS_STATE ;
}());
foo.ACCESS_STATE = ACCESS_STATE ;
Why is this not the default behavior of the compiler to generate a re-useable kind of thing that can be a referential substitute for the values in the enum (which is what is generally intended by using an enumeration)?
Upvotes: 3
Views: 702
Reputation: 51629
From the documentation:
Const enums can only use constant enum expressions and unlike regular enums they are completely removed during compilation.
To have enum code in javascript, you need to remove const
from export const enum
. Alternatively, you can compile your code with --preserveConstEnums
option.
Upvotes: 5