Skip to content

Ordering sensitivity of exported enums and usages #2594

Closed
@robertknight

Description

@robertknight

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?

Activity

mhegazy

mhegazy commented on Apr 2, 2015

@mhegazy
Contributor

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.:

var value = Color.Red;

export enum Color {
    Red = 1 << 1
}
``
would emit:
```js
 var value = Color.Red;
    (function (Color) {
        Color[Color["Red"] = 1 << 1] = "Red";
    })(exports.Color || (exports.Color = {}));
    var Color = exports.Color;

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.

added this to the TypeScript 1.5 milestone on Apr 2, 2015
robertknight

robertknight commented on Apr 3, 2015

@robertknight
Author

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

chinhodado commented on Jun 4, 2015

@chinhodado

So is the workaround export const enum Color?

mhegazy

mhegazy commented on Jun 4, 2015

@mhegazy
Contributor

@chinhodado it is up to you, using const enums means that the enum will be completely erased, and constants propagated at compile time. so Color does not exist at runtime.

chinhodado

chinhodado commented on Jun 4, 2015

@chinhodado

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

mhegazy commented on Jun 4, 2015

@mhegazy
Contributor

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;

locked and limited conversation to collaborators on Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing code

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @robertknight@chinhodado@mhegazy

        Issue actions

          Ordering sensitivity of exported enums and usages · Issue #2594 · microsoft/TypeScript