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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Babel with Typescript produces functions for an empty namespaces. #14947

Closed
1 task
qisoft opened this issue Sep 18, 2022 · 2 comments 路 Fixed by #14952
Closed
1 task

[Bug]: Babel with Typescript produces functions for an empty namespaces. #14947

qisoft opened this issue Sep 18, 2022 · 2 comments 路 Fixed by #14952
Labels
area: typescript i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@qisoft
Copy link

qisoft commented Sep 18, 2022

馃捇

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

export namespace A {}
export namespace B {
    interface C {} 
}

Configuration file name

.babelrc

Configuration

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "modules": false
            }
        ],
        [
            "@babel/preset-typescript",
            {
                "allowNamespaces": true
            }
        ]
    ]
}

Current and expected behavior

Current behavior:
Babel produces consts and empty functions for these namespaces.

Expected behavior:
Babel should ignore them the same way as TypeScript compiler does.

This is useful when you have a React component and a namespace with the same name containing the Props interface inside. This allows you to import { ComponentName } from './ComponentName' without additional import for Props type by accessing it with ComponentName.Props. Such namespace exists in types scope only and should not affect the resulting code. There is no sense and use of such function produced from these empty namespaces, it's just garbage.

Environment

  • Babel version: 7.19.1
  • Node version: 16

Possible solution

No response

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @qisoft! 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.

@qisoft
Copy link
Author

qisoft commented Sep 18, 2022

PS: I know that I can use declare namespace { ... } instead of just namespace but this doesn't work for the nested namespace (e.g.

export namespace A {
  export declare namespace B {
    export interface C { }
  }
}

It still produces an empty function for B inside A:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.A = void 0;
let A;
exports.A = A;

(function (_A) {
  let B;

  (function (_B) {})(B || (B = _A.B || (_A.B = {})));
})(A || (exports.A = A = {}));

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Dec 21, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants