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
Typescript: fields defined with definite assignment assertion are removed with TS compiler flag useDefineForClassFields
set
#12146
Comments
Hey @akphi! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly. If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite." |
useDefineForClassFields
set
It seems like our logic in the plugin is wrong: babel/packages/babel-plugin-transform-typescript/src/index.js Lines 72 to 90 in 3ec4537
We unified the error handling for initializers in |
Please note that we should keep the field only when |
@nicolo-ribaudo Thanks for giving the opportunity. I will give it a shot! |
In case you need any help with getting started (feel free to ignore this message otherwise): If it is the first time that you contribute to Babel, follow these steps: (you need to have
|
@nicolo-ribaudo that was super helpful :) I created an MR, please could you take a look? Also, if this is merged, could we do a release for |
Bug Report
Current behavior
If I have a field defined using
!
, for example:babel
usingpreset-typescript
will transform it to this (REPL link):I think is the behavior currently being implemented, given this test case.
This is the same behavior I observe in Typescript version < 3.7, see this playground link
But Typescript 3.7 comes with the new compiler flag
useDefineForClassFields
(doc). With this option enabled, see this playground link, for the same input above, what Typescript generates is:Expected behavior
I think I get the decision behind stripping out fields defined using definite assignment assertion, so conceptually, they are no different than fields defined using
declare
. However, for my use case, I would really likebabel
to be in sycn with Typescript here for the case whereuseDefineForClassFields
is set totrue
, that is I want to fields to be present and initialized toundefined
. I think I have seen exception made in the past for not stripping away!
fields like in #8238. I wonder if we can add a new setting topreset-typescript
or make another exception.Environment
I thought the above REPLs and playgrounds should suffice, but in case you need a minimal repo, I prepared this repo. For this the important bits in my configs are:
Additional context
If this is appropriate, I want to mention my use case. I have some classes where the constructor is not a good place to initialize some of the fields, but I want to keep defining them using
!
because they should not be nullable (If only JS/TS support overloading constructors, this would be a non-problem). I'm also usingmobx
, which creates an observable wrapper around fields so the recommended place to set this is in the constructor, andmobx
cannot really wrap fields that do not exist becausebabel
strips away the field defined with!
during build time.mobx
used to work fine in the past because they use the decorator form, i.e.@observable age!: number
which at the time worked because of #8238Also, another totally coincidental thing is I used to have
@babel/plugin-proposal-class-properties
running beforepreset-typescript
so it emits!
fields and things work just fine, but recently, because I usedeclare
field likedeclare name: string
, I have to make surepreset-typescript
runs before@babel/plugin-proposal-class-properties
. So this problem surfaces, then on that topic, there's #10311Anyhow, this has been a long report, please let me know what you think or what I can (maybe) help with since I'm still pretty inexperience. Thanks a lot!
The text was updated successfully, but these errors were encountered: