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

Namespaces fail to merge with enum, w/ misleading error message #11460

Closed
m93a opened this issue Apr 21, 2020 · 4 comments
Closed

Namespaces fail to merge with enum, w/ misleading error message #11460

m93a opened this issue Apr 21, 2020 · 4 comments
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@m93a
Copy link

m93a commented Apr 21, 2020

Bug Report

Current Behavior
Set up a TypeScript repository with Babel and plugin-transform-typescript. Add this code:

Input Code

export enum Foo
{
    a, b, c, d
}

export namespace Foo
{
  export function bar()
  {
    return "bar"
  }
}

Expected behavior/code
It should compile just fine. In TypeScript this just works: Playground Link

Actual behavior
The compiler throws a very misguiding error:

./src/Foo.ts
SyntaxError: Foo.ts: Namespace not marked type-only declare. Non-declarative namespaces are only supported experimentally in Babel. To enable and review caveats see: https://babeljs.io/docs/en/babel-plugin-transform-typescript
  4 | }
  5 | 
> 6 | export namespace Foo
    |                  ^^^
  7 | {
  8 |   export function bar()
  9 |   {

Changing the order of enum and namespace declarations results in this error:

./src/Foo.ts
TypeError: Foo.ts: Duplicate declaration "Foo"
   8 | }
   9 | 
> 10 | export enum Foo
     |             ^^^
  11 | {
  12 |     a, b, c, d
  13 | }

Babel Configuration

  • The repository was created with:
yarn create react-app --template=typescript
yarn add @babel/plugin-transform-typescript react-app-rewired customize-cra

Then add this to config-overrides.js:

const {
    override,
    addExternalBabelPlugin
  } = require("customize-cra");

module.exports = override(
    addBabelPlugin([
        "@babel/plugin-transform-typescript",
        { allowNamespaces: true }
    ])
);

Environment

  • Babel version(s): v7.9.0
  • Node/npm version: v13.13.0
  • OS: elementary
  • Monorepo: no
  • How you are using Babel: via react-scripts, webpack

Walkaround
If you need to keep the exact structure in your project, you can do this:

export enum Foo_
{
    a, b, c, d
}

export type Foo = Foo_

export namespace Foo
{
  export const a = Foo_.a
  export const b = Foo_.b
  export const c = Foo_.c
  export const d = Foo_.d
  export function bar()
  {
    return "bar"
  }
}

(We can't use a destructuring assignment because of #11461)

Proposed Solution
At least the misleading errors should be fixed as soon as possible. Thinking that the plugin loaded incorrectly caused me a lot of pain and several wasted hours. However, the feature itself is important too and I'd really love to see it implemented.

@babel-bot
Copy link
Collaborator

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

@m93a m93a changed the title Namespaces fail to merge with enum Namespaces fail to merge with enum, w/ misleading error message Apr 21, 2020
@nicolo-ribaudo
Copy link
Member

The problem is that you have two instances of the typescript plugin: the one enabled by CRA, and the one added by you. The one enabled by CRA finds namespace and throws.

I think the CRA Babel preset supports a typescript: false option to disable their instance of the plugin.

@m93a
Copy link
Author

m93a commented Apr 22, 2020

Hey,
Thank you for your reply. Why do I have two instances of the typescript plugin? CRA also compiles TypeScript using plugin-transform-typescript? I don't see any CRA preferences that could help me disable TypeScript in an already created project. Could you please help me out with this?

EDIT: Isn't it weird that interface+namespace merging works just fine, but enum+namespace doesn't?

@liuxingbaoyu
Copy link
Member

Seems to have been fixed.

@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 17, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants