Skip to content

Double Emit of Contructor when using Getter Decorators #53448

@Matchlighter

Description

@Matchlighter

Bug Report

With the implementation of Stage 3 Decorators in TS 5, using a getter decorator can result in the content of the constructor being duplicated in the emitted code, for example:

            constructor() {
                this.computedVal;
                console.log("As will this");
                __runInitializers(this, _instanceExtraInitializers);
                this.computedVal;
                console.log("As will this");
            }

🔎 Search Terms

Constructor duplicate decorators getter

🕗 Version & Regression Information

Started seeing this when I updated a library from TS 5.0-beta to TS 5.0.2.

This changed between versions 5.0-beta and 5.0.2. Not applicable to prior versions.

⏯ Playground Link

Playground link with relevant code

💻 Code

function computed(target, ctx: ClassGetterDecoratorContext) {
    ctx.addInitializer(function () {
        console.log("INIT")
    })
    return target
}

class Bla {
    // Things seem to work as expected if a field or accessor is also defined,
    //   but additional getters/setters or methods continue to see the double emission
    // name: string = ""

    @computed
    get computedVal() {
        console.log("This will print twice")
        return 3
    }

    constructor() {
        this.computedVal
        console.log("As will this")
    }
}

new Bla()

🙁 Actual behavior

Constructor logic is duplicated:

            constructor() {
                this.computedVal;
                console.log("As will this");
                __runInitializers(this, _instanceExtraInitializers);
                this.computedVal;
                console.log("As will this");
            }

🙂 Expected behavior

Constructor logic should not be duplicated:

            constructor() {
                __runInitializers(this, _instanceExtraInitializers);
                this.computedVal;
                console.log("As will this");
            }

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions