-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
SWC: this
returns an empy object in class constructors when fields are not initialized
#32356
Comments
this
returns an empy object in class constructors when fields are not initialized
Just an update: looks like SWC follows tsc behavior where uninitialized fields are removed. While they're just following a well-established convention, it doesn't really make a lot of sense to me. Consider this minimal class in plain JS: class User {
name
id
} If you class User {
name: string
id: number
} ...and it does when SWC is not used. So looks like Next.js used to handle this transformation in a certain way until v11 and now it can behave in two different ways depending on which setting is used. That can be a major obstacle to migration for codebases that rely on this behavior. |
This should be fixed in swc-project/swc#3459 |
Still not working as of 12.1.4 |
See https://swc.rs/docs/migrating-from-tsc#usedefineforclassfields |
You can set |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
12.0.7
What version of Node.js are you using?
14.16.0
What browser are you using?
Chrome
What operating system are you using?
Windows 10
How are you deploying your application?
not relevant to this issue
Describe the Bug
When accessing
this
in a class constructor with uninitialized fields, I'm getting an empty object instead of an object where the fields are present butundefined
. This behavior is only present when SWC is enabled (i.e. nobabel.config.js
). When the minimumbabel.config.js
file (module.exports = { presets: ['next/babel'] }
) is present, it works just fine.Expected Behavior
this
should return an object with all uninitialized fields present and set toundefined
.To Reproduce
Run
next dev
with the following files (tsconfig.json
was created automatically, so I'm omitting it):/src/pages/index.tsx
package.json
The text was updated successfully, but these errors were encountered: