Skip to content
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

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

Closed
magic-akari opened this issue Aug 11, 2024 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@magic-akari
Copy link
Contributor

magic-akari commented Aug 11, 2024

🔎 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

@magic-akari
Copy link
Contributor Author

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.

@whzx5byb
Copy link

whzx5byb commented Aug 11, 2024

Duplicate of #45995, #55132.

@typescript-bot
Copy link
Collaborator

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

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 15, 2024
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