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

Enable TS type-only class fields by switching TS Babel from preset to transform plugin #9235

Closed

Conversation

pekala
Copy link

@pekala pekala commented Jun 29, 2020

Fixes #8918

Type-only class fields using the declare keyword were added by TS 3.7. CRA currently fails when it encounters them with the following error:

TypeScript 'declare' fields must first be transformed by @babel/plugin-transform-typescript.
If you have already enabled that plugin (or '@babel/preset-typescript'), make sure that it runs before any plugin related to additional class features:
 - @babel/plugin-proposal-class-properties
 - @babel/plugin-proposal-private-methods
 - @babel/plugin-proposal-decorators

Declare fields are useful when using React context in class components, e.g.:

class MyComponent extends Component<MyProps> {
    static contextType = MyContext
    declare context: React.ContextType<typeof MyContext>

This PR switches from Babel TS preset to Babel TS transform plugin in order to run the babel transform before @babel/plugin-proposal-class-properties. It also switches the support for declare fields by using the allowDeclareFields option on the plugin. This will be the default for Babel 8, but for now, we need to use the option.

The preset to plugin switch should have no other effect since the babel TS preset wraps only one plugin and provides little more than the transform plugin itself.

@facebook-github-bot
Copy link

Hi @pekala!

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file.

In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@sakalys
Copy link

sakalys commented Jul 15, 2020

So what are the things that broke?

@pekala
Copy link
Author

pekala commented Jul 18, 2020

@sakalys It's most likely outdated yarn.lock.cached - Looking at this PR I think that file needs to be updated. But I'm not sure what's the procedure here, would appreciate some guidance.

@sakalys
Copy link

sakalys commented Jul 20, 2020

@pekala, one thing to try is to update the PR by merging in the new master branch into your own. After that, make sure to run yarn command, to generate a new "yarn.lock" file.

@pekala
Copy link
Author

pekala commented Jul 20, 2020

Hmm, I don't think it's that simple. yarn.lock files are ignored in this repo, there is this yarn.lock.cached file, but it's unclear to me what how it's updated.

@stale
Copy link

stale bot commented Aug 22, 2020

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added stale and removed stale labels Aug 22, 2020
@stale
Copy link

stale bot commented Oct 4, 2020

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added stale and removed stale labels Oct 4, 2020
@dgreensp
Copy link

dgreensp commented Nov 6, 2020

I ran into this issue and am interested in this fix

@stale
Copy link

stale bot commented Dec 19, 2020

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Dec 19, 2020
@ekilah
Copy link

ekilah commented Dec 23, 2020

Looks like there are conflicts now, but if this is all that needs to happen to be able to use declare I too am interested still. Just hopping in to fight the bot

@stale stale bot removed the stale label Dec 23, 2020
@stale
Copy link

stale bot commented Jan 9, 2022

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added stale and removed stale labels Jan 9, 2022
@pekala pekala closed this Jul 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript declare field causes transpile to fail
5 participants