Skip to content

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

Closed
@kayahr

Description

@kayahr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions