Skip to content

Compiler should error when using an Enum before its declaration #11935

@olov

Description

@olov

TypeScript Version: nightly (2.1.0-dev.20161028)

Code

const v: Color = Color.Green;
enum Color { Red, Green, Blue }

which compiles to

var v = Color.Green;
var Color;
(function (Color) {
    Color[Color["Red"] = 0] = "Red";
    Color[Color["Green"] = 1] = "Green";
    Color[Color["Blue"] = 2] = "Blue";
})(Color || (Color = {}));

Expected behavior:
Error on line 1: Enum Color used before its declaration.

Actual behavior:
No compiler error. Run-time error: Uncaught TypeError: Cannot read property 'Green' of undefined

Comments:
Rearranged compiler output would also be ok instead of an error:

var Color;
(function (Color) {
    Color[Color["Red"] = 0] = "Red";
    Color[Color["Green"] = 1] = "Green";
    Color[Color["Blue"] = 2] = "Blue";
})(Color || (Color = {}));
var v = Color.Green;

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions