Skip to content

"used before its declaration" errors not reported when variable/enum/class is used as decorator parameter on class member #50349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zivarah opened this issue Aug 17, 2022 · 1 comment · Fixed by #50372
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@zivarah
Copy link

zivarah commented Aug 17, 2022

Bug Report

🔎 Search Terms

decorator
enum
used before its declaration
TS2448, TS2449, TS2450
isolatedModules
preserveConstEnums

🕗 Version & Regression Information

⏯ Playground Link

Workbench Repro

💻 Code

// @experimentalDecorators: true

function methodDecorator(value: string): any {
	return () => {};
}

//@methodDecorator(MyEnum.value1) // Correctly reports Enum 'MyEnum' used before its declaration (TS2450)
export class MyClass {
    @methodDecorator(MyEnum.value1) // No error reported
    public foo(): void {
    }
}

enum MyEnum {
    value1 = "value1",
}

🙁 Actual behavior

  • No TS2450 "Enum 'MyEnum' used before its declaration" is reported.
  • At runtime, this code crashes during the initial script execution:
C:\repos\TS2450_decorator_repro\program.js:19
        methodDecorator(MyEnum.value1) // No error reported
                               ^

TypeError: Cannot read properties of undefined (reading 'value1')
    at C:\repos\TS2450_decorator_repro\program.js:19:32
    at Object.<anonymous> (C:\repos\TS2450_decorator_repro\program.js:22:2)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

🙂 Expected behavior

TS2450 (and other similar errors like 2448 for Block-scoped variables or 2449 for classes) should be correctly reported.

@zivarah
Copy link
Author

zivarah commented Aug 17, 2022

This issue caught us off guard when we switched to using isolatedModules. This causes const enums to be preserved as if preserveConstEnums was set, which meant that a value that used to compile in stopped doing so, and no error was raised during build due to this issue.

In the code example given in the issue above, using 4.7:

Before isolatedModules:

var MyClass = /** @class */ (function () {
    function MyClass() {
    }
    MyClass.prototype.foo = function () {
    };
    __decorate([
        methodDecorator("value1" /* MyEnum.value1 */)
    ], MyClass.prototype, "foo");
    return MyClass;
}());
exports.MyClass = MyClass;

After isolatedModules:

var MyClass = /** @class */ (function () {
    function MyClass() {
    }
    MyClass.prototype.foo = function () {
    };
    __decorate([
        methodDecorator(MyEnum.value1)
    ], MyClass.prototype, "foo");
    return MyClass;
}());
exports.MyClass = MyClass;
var MyEnum;
(function (MyEnum) {
    MyEnum["value1"] = "value1";
})(MyEnum || (MyEnum = {}));

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Aug 18, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants