Skip to content

used before its declaration error when preserveConstEnums=trueΒ #53019

Closed
@frigus02

Description

@frigus02

Bug Report

πŸ”Ž Search Terms

preserveConstEnums, TS2450, used before its declaration

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried: v4.9.5

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export const config = {
    a: Foo.A,
//     ^^^ Enum 'Foo' used before its declaration. (2450)
};

export const enum Foo {
    A = 1,
}

πŸ™ Actual behavior

There is a "used before its declaration" error with preserveConstEnums: true, and no error with preserveConstEnums: false.

As far as I can see this behavior was introduced including a test case in #25822.

πŸ™‚ Expected behavior

Since the enum reference is still inlined, I don't expect a "used before its declaration" error. Alternatively I may expect the "used before its declaration" error for all const enum declarations independently from preserveConstEnums. Basically, I don't understand why preserveConstEnums changes things.

I kinda understand why we want an error in the test case. The declaration after the return statement is unreachable, which might not be what the author wanted:

function foo2() {
return 0 /* E.A */;
var E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
}

I don't understand the reason in my code. I might just be missing something, though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions