-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[Bug]: export namespace compiled failed in create-react-app #15875
Comments
Hey @yangguansen! 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. |
Hello, export namespace API {
export namespace Foo1 {
export namespace Bar1 {
export type Payload = {
// ...
}
export type Response = {
// ...
}
}
export namespace Bar2 {
// ...
}
}
export namespace Foo2 {
// ...
}
} We would type some variable like this: const foo:API.Foo1.Bar1.Payload = 'bar' I am not sure about 0.68.4, but in 0.72.4 babel would compile it to something like: var API;
exports.API = API;
(function (_API) {
export var Foo1; //this line would error out
export var Foo2;
}
) I figured out I can bypass this error (hopefully it won't be needed in the future!) by not-really-nesting namespaces: export namespace API.Foo1 {
export namespace Bar1 {
export type Payload = {
// ...
}
export type Response = {
// ...
}
}
export namespace Bar2 {
// ...
}
}
export namespace API.Foo2 {
// ...
} It does work, but in places where I had more nestings I would have to specify them all out, which is less than ideal: export namespace API.Foo1.Bar1.Baz1 {
export type Payload = {
//...
}
} I hope this helps with the investigation! |
@liuxingbaoyu I noticed that you assigned this issue to yourself, but "Would you like to work on a fix?" has been selected :) |
Sorry that i selected the checkbox "Would you like to work on a fix?", I hope that who can fix this, many thanks! |
@yangguansen If you are interested, you are welcome to submit a PR to fix this :) (If you cannot don't worry, somebody else will do it) The bug is probably in babel/packages/babel-plugin-transform-typescript/src/namespace.ts Lines 226 to 233 in 0c8dfc1
export namespace , if its contents are not transformed (i.e. if it only contains type-level declarations, such as interfaces) we simply do nothing. Instead, if transformed is null , we should remove it by removing namespaceTopLevel[i] from namespaceTopLevel .
If it is the first time that you contribute to Babel, follow these steps: (you need to have
|
Wow, appreciate for giving these amazing suggestions to me , I will take a look at this to research the related code , and this is my first time to touch Babel source code, I'm not sure I can solve the issue in future days but i will take some effort on this, welcome other guys to take this as well :) |
Feel free to ask for help here if needed! |
Oops I didn't notice this before. :) |
I simply add a sentence at babel/packages/babel-plugin-transform-typescript/src/namespace.ts but when i run
Could someone let me know what's the reason about this test timeout error? |
Feel free to ignore that failure -- for some reason that test sometimes fails but only when running all the tests, and never on CI 😅 |
PR:#15882 |
💻
How are you using Babel?
babel-loader (webpack)
Input code
test-ts.ts
App.tsx
Configuration file name
.babelrc
Configuration
No response
Current and expected behavior
Reproduce steps:
create a new project from create-react-app@5
add test-ts.ts file like above and import this file at a *.tsx file
then it report an error, but i think it shouldn't has error.
Current behavior:
Expected behavior
it shouldn't has error report
Environment
System:
OS: macOS 12.0.1
Binaries:
Node: 16.14.2 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 8.5.0 - /usr/local/bin/npm
npm Packages:
@babel/plugin-proposal-private-property-in-object: ^7.21.11 => 7.21.11
babel-loader: 9.1 => 9.1.3
eslint: ^8.46.0 => 8.47.0
Possible solution
No response
Additional context
reproduce lib:
https://github.com/yangguansen/typescript-bug-report
The text was updated successfully, but these errors were encountered: