Skip to content

Static properties are not correctly transpiled when using decorators and target ES2022 #52258

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
kayahr opened this issue Jan 16, 2023 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@kayahr
Copy link

kayahr commented Jan 16, 2023

Bug Report

🔎 Search Terms

static, property, decorator, es2022, undefined

🕗 Version & Regression Information

This is the behavior in every version which supports ES2022 target I tried, and I reviewed the FAQ for entries about "static"

⏯ Playground Link

Playground Link

💻 Code

// @target: es2022
// @lib: es2015,dom
// @experimentalDecorators: true

function bar(target: Function): void {
}

@bar
class Foo {
    public static a = 1;
    public static b = Foo.a * 2;
}

console.log(Foo.a)
console.log(Foo.b)

🙁 Actual behavior

With target 2022 the code compiles to this JavaScript:

let Foo = Foo_1 = class Foo {
    static { this.a = 1; }
    static { this.b = Foo_1.a * 2; }
};

Or to this when useDefineForClassFields setting is set to true:

let Foo = Foo_1 = class Foo {
    static a = 1;
    static b = Foo_1.a * 2;
};

Both cases are wrong and throw a runtime error because Foo_1 is undefined.

When compiler target is set 2021 then it works because static properties are then initialized after the class has been created, so Foo_1 exists at this point.

When no decorator is used then this also works because then the transpiler does not create a Foo_1 variable and the static initializer uses Foo instead which exists at this point.

🙂 Expected behavior

There should be no undefined error in the generated code. This could be achieved by using Foo in the static initializer instead of Foo_1.

@MartinJohns
Copy link
Contributor

MartinJohns commented Jan 16, 2023

I feel I've seen this a couple of times already. Most likely it'll be fixed with the upcoming support for decorators.

Edit: sounds like a duplicate of #52004.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 25, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants