Closed
Description
With default compile settings, the following works under TS 1.4 but fails under TS 1.5.0-alpha due to Color being undefined until the code generated for the exported enum has been evaluated because the default output for enums changed from emitting constants to references to 'Color.Red'.
// compile with 'tsc -m enum.ts'
var value = Color.Red;
export enum Color {
Red
}
I'm unsure if this is considered a bug or simply a compatibility hazard to document?
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
mhegazy commentedon Apr 2, 2015
This worked because in 1.4 we aggressively inlined enum values. Now this only happens for const enums and not regular enums.
It is also worth nothing that the general case never worked in 1.4, e.g.:
which would have failed as Color is not defined.
I think this is a good candidate to be handled in #21, making it an error to reference a non-const enum before its declaration.
robertknight commentedon Apr 3, 2015
Proposal in #21 sounds good to me. I was impressed that this was the only breakage in a 20K loc codebase updating from TS 1.4 to 1.5 - enjoying the ES6 awesome sauce.
chinhodado commentedon Jun 4, 2015
So is the workaround
export const enum Color
?mhegazy commentedon Jun 4, 2015
@chinhodado it is up to you, using
const enum
s means that the enum will be completely erased, and constants propagated at compile time. soColor
does not exist at runtime.chinhodado commentedon Jun 4, 2015
That certainly is not what I want, since I also need reverse enum lookup (like
var color = Color[1]
).In short, how do I get back pre-1.5 enums semantics? Or what are the recommended ways to create and work with enums in 1.5+?
mhegazy commentedon Jun 4, 2015
Your only workaround is to make sure the enum is defined before it is used. The compiler should warn you about that (tracked issue #21), hopefully we can get this in the next (TypeScript 1.6) release;