Skip to content

The behavior of class Parameter Properties changes with the es target #59589

Closed as not planned

Description

@magic-akari

🔎 Search Terms

  • class
  • constructor parameter
  • useDefineForClassFields
  • initialization

📌 Related issues

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?useDefineForClassFields=true&target=7#code/PTAEAEBcEMCcHMCmkBcpEGcBMAGXAoECAVw0QBFEAzASwDtEAxAe1gGEAbaDDRmxDgBMMaSLGKJ8AYy49QAFQCeAB0QBlKbBrLIABTjQAtskSxdsZqtiR+GUAG98oZ6CJQMAWhrw6rSS9AqZmZQAF5QSAALGgwAOgAjOABuJxcpZjoMMWIpSFYACmVieI4aKVBE2DRoOkUASgdUgOd0zOYORFiOZnh8gCJaOmgODkU+gBoI6Lig5jqUgIBffGX8BgB3BRV1TW09A2NIU3NLUxtMfoA3YYk++aA

💻 Code

// @target: es2020
// @useDefineForClassFields: true
class TypeScriptParameterProperties {
    // @ts-ignore
    foo = this.bar;
    constructor(public bar: any) {
        console.log("finally", this.foo);
    }
}

new TypeScriptParameterProperties("value");
// @target: esnext
// @useDefineForClassFields: true
class TypeScriptParameterProperties {
    // @ts-ignore
    foo = this.bar;
    constructor(public bar: any) {
        console.log("finally", this.foo);
    }
}

new TypeScriptParameterProperties("value");

🙁 Actual behavior

Different compilation options will output different values.

es2020: "finally", "value"
esnext: "finally", undefined

🙂 Expected behavior

They should output the same value.

Additional information about the issue

Activity

  1. magic-akari commented on Aug 11, 2024

    @magic-akari
    ContributorAuthor

    Here is another example, which does not use ts-ignore and ts does not report any errors.

    // @target: es2020
    // @useDefineForClassFields: true
    class TypeScriptParameterProperties {
        foo = console.log("foo");
        constructor(public bar: any = console.log("bar")) {
            console.log("finally");
        }
    }
    
    new TypeScriptParameterProperties();

    Playground

    // @target: esnext
    // @useDefineForClassFields: true
    class TypeScriptParameterProperties {
        foo = console.log("foo");
        constructor(public bar: any = console.log("bar")) {
            console.log("finally");
        }
    }
    
    new TypeScriptParameterProperties();

    Playground

    The order in which they print out foo and bar is different.

  2. whzx5byb commented on Aug 11, 2024

    @whzx5byb

    Duplicate of #45995, #55132.

  3. typescript-bot commented on Aug 15, 2024

    @typescript-bot
    Contributor

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

  4. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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