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

Useless empty class fields are generated when using parameter properties and target=ESNext #45235

Closed
angryziber opened this issue Jul 29, 2021 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@angryziber
Copy link

Bug Report

  class MenuItem {
    constructor(public name: string) {}
  }

Compiling this with -t ESNext produces:

class MenuItem {
    name;
    constructor(name) {
        this.name = name;
    }
}

The name field above also confuses some next stage build tools that don't support class fields yet, e.g. the popular acorn that is used by Svelte compiler (sveltejs/svelte#6592)

However, compiling with -t ES2021 produces more compact code without useless field declaration:

class MenuItem {
    constructor(name) {
        this.name = name;
    }
}

🔎 Search Terms

constructor shorthand

🕗 Version & Regression Information

Version 4.3.5 & next

  • This changed between versions 4.2.x and 4.3.x
@IllusionMH
Copy link
Contributor

ESNext uses useDefineForClassFields by default
See #45076

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 29, 2021
@fatcerberus
Copy link

Just to add because I feel this is important: in an environment that natively supports class fields, the declared field is not useless - behavior actually differs between the two snippets because ECMAScript dictates [[Define]] semantics for fields.

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants