-
-
Notifications
You must be signed in to change notification settings - Fork 27k
Typescript declare field causes transpile to fail #8918
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
Comments
also experiencing this in a very-recently-ejected app |
This issue 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. |
this is still an issue afaik |
Yes, still an issue. And possibly this is the issue that also prevents from using I can work around this problem by either using the context parameter directly from the constructor parameters or by using it in |
I think the fix is something along the lines of #9235 This worked for my non-CRA project suffering from the same issue. |
@pekala Your PR looks great! What can I change in my existing CRA project to get it to work? I'm not familiar with how this is all set up, but I don't see anything about Babel at all in the root of my project; is |
CRA hides and manages those for you, so unless you want to eject or play around with something like Craco, you have to wait for that PR to land. |
Would be great to see this fixed, not really able to use Context API at this point in my typescript project :/ |
I've tried to fix it with |
This issue 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. |
@pekala At a deeper level, how does that work, though? Does CRA use a hidden config file in the resulting project, or… what? More curious about the implementation than anything else |
This issue 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. |
I have also encountered this. I am implementing an error boundary which has to be a class component and need to use my app's context. |
I also meet this question,It's via babel plugins add -> @babel/plugin-transform-typescript and options -> "allowDeclareFields": true
will need -> yarn add @babel/plugin-transform-typescript -D |
Hey guys, just encountered this issue myself, can we hope this to be fixed soon? |
The actual fix after ejecting seems to be simple: I can't tell if this fix would break anything for anyone else though if it's integrated into the Typescript template. If anyone can verify that, I can create a PR for it. |
You can work around this using CRACO. I'll share my configuration, but first a warning: I barely understand web pack's configuration and figured this out through trial and error. Be wary. // craco.config.js
const {
loaderByName,
getLoader,
} = require("@craco/craco");
module.exports = {
webpack: {
configure: function (webpackConfig) {
const babelLoader = getLoader(
webpackConfig,
loaderByName("babel-loader")
).match.loader;
babelLoader.options.presets.push([
"@babel/preset-typescript",
{allowDeclareFields: true},
]);
return webpackConfig;
}
}
}; |
This comment was marked as duplicate.
This comment was marked as duplicate.
Is there a workaround without ejecting or additional dependencies? If you leave out the declaration it is literally telling you to add it, but it can't actually be used... |
The suggested usage of using: `declare context: React.ContextType<typeof HostContext>` is not working due to the open issue: facebook/create-react-app#8918 During build the flowing error is blocking: `'declare' fields must first be transformed by @babel/plugin-transform-typescript` As there is no proper workaround the decision was made to pass props instead.
The suggested usage of using: `declare context: React.ContextType<typeof HostContext>` is not working due to the open issue: facebook/create-react-app#8918 During build the flowing error is blocking: `'declare' fields must first be transformed by @babel/plugin-transform-typescript` As there is no proper workaround the decision was made to pass props instead.
Describe the bug
Using typescript declare to fix the type of the context field in class components causes the build to fail with
Did you try recovering your dependencies?
Yes
Which terms did you search for in User Guide?
typescript declare transpile
Environment
Steps to reproduce
npm start
ornpm run build
Expected behavior
That the build will succeed
Actual behavior
Build fails with
Reproducible demo
https://github.com/benwiles1/broken-cra
The text was updated successfully, but these errors were encountered: