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

[@babel/preset-typescript] Namespace alias (native TS feature) is not supported #12345

Closed
1 task
alker0 opened this issue Nov 11, 2020 · 8 comments · Fixed by #13528
Closed
1 task

[@babel/preset-typescript] Namespace alias (native TS feature) is not supported #12345

alker0 opened this issue Nov 11, 2020 · 8 comments · Fixed by #13528
Labels
area: typescript help wanted i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@alker0
Copy link

alker0 commented Nov 11, 2020

Bug Report

  • I would like to work on a fix!

import RenamedModule = OriginalModule is native Typescript's feature for creating module alias but @babel/preset-typescript does not allow this syntax.

Current behavior

@babel/preset-typescript gives an error.

Error: `import =` is not supported by @babel/plugin-transform-typescript
Please consider using `import <moduleName> from '<moduleName>';` alongside Typescript's --allowSyntheticDefaultImports option.

Input Code

declare module LongNameModule {
  export type SomeType = number;
}
import AliasModule = LongNameModule;

const some: AliasModule.SomeType = 3;
console.log(some);

Expected behavior

It outputs: 3.

Using tsc and ts-node, it is compiled as expected.

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: babel.config.js
{
  presets: [
    [
      '@babel/env',
      {
        modules: false,
        targets: {
          esmodules: true,
        },
      },
    ],
    [
      '@babel/typescript',
      {
        allowDeclareFields: true,
      },
    ],
  ],
};

Environment

  • Babel version: v7.12.3
  • Node/npm version: Node 14/yarn 1.22.10
  • OS: alpine linux (in docker)
  • Monorepo: no
  • How you are using Babel: cli

Possible Solution

I have no idea.

@babel-bot
Copy link
Collaborator

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

@mischnic
Copy link
Contributor

See https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats:

This plugin does not support export = and import =, because those cannot be compiled to ES.next. These are a TypeScript only form of import/export.

Workarounds:

@alker0
Copy link
Author

alker0 commented Nov 11, 2020

This input code is in only one file.

// some-file.ts
declare module LongNameModule {
  export type SomeType = number;
}

import AliasModule = LongNameModule; // this is also in some-file.ts

const some: AliasModule.SomeType = 3;
console.log(some);

I tried this command:
tsc --target ESNEXT --module ESNext --skipLibCheck --outDir dist/ some-file.ts
And that typescript file is compiled into the following code:

// some-file.js
const some = 3;
console.log(some);

This is just to create module name alias in same file, and it also works when target is ESNext.

It does not seem to be same feature that is import = for importing external commonJS modules.

I want to use this feature for development of the browser scripts. Can this preset not support this feature?

@alker0
Copy link
Author

alker0 commented Nov 30, 2020

This is the official documentation: https://www.typescriptlang.org/docs/handbook/namespaces.html#aliases

Not to be confused with the import x = require("name") syntax used to load modules, this syntax simply creates an alias for the specified symbol. You can use these sorts of imports (commonly referred to as aliases) for any kind of identifier, including objects created from module imports.

The babel documentation @mischnic quoted seems to mention loading modules, but this is not what I mention.

I should have called this feature "Namespace alias" rather than "Module alias".

@alker0 alker0 changed the title [@babel/preset-typescript] Module alias (native TS feature) is not supported [@babel/preset-typescript] Namespace alias (native TS feature) is not supported Nov 30, 2020
@nicolo-ribaudo
Copy link
Member

This is something that we can support. When we wrote those docs, what we wanted to disallow was import foo = require("...") and export =. import foo = bar is a different feature that has nothing to do with ESM (it just re-uses the keyword).

@orty
Copy link

orty commented Apr 20, 2021

Hi,

Is there any news regarding this ? I looked around for a plugin to use as a workaround until Babel support namespace aliases but no luck :(

@ilhanali
Copy link

I'm struggling with the same problem right now.

@colinaaa
Copy link
Contributor

colinaaa commented Jul 3, 2021

I would like to work on this!

colinaaa added a commit to colinaaa/babel that referenced this issue Jul 3, 2021
support `import alias = Namespace`, reject `import alias = require('foo')`

fix babel#12345
nicolo-ribaudo pushed a commit that referenced this issue Aug 3, 2021
support `import alias = Namespace`, reject `import alias = require('foo')`

fix #12345
@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 Nov 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript help wanted 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.

8 participants