Skip to content

[Bug]: import type {} from 'some-module' results in invalid typescript #14250

@nicojs

Description

@nicojs

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

import type { } from 'some-module';

Configuration file name

No response

Configuration

No configuration file

Current and expected behavior

Using this code:

const { parseAsync } = require("@babel/core");
const generate = require("@babel/generator").default;

async function main() {
  const parsed = await parseAsync('import type {} from "some-module";', {
    filename: "main.ts",
    presets: [require.resolve("@babel/preset-typescript")],
    plugins: [],
  });
  console.log(
    generate(parsed, {sourceMaps: false}).code
  );
}

main().catch(console.error);

Results in: import type "some-module";

Which is invalid typescript.

Environment

  • Babel: 7.16.12

Possible solution

I've noticed that the AST for:

import type {} from "some-module";

And

import type "some-module";

is the exact same. See https://astexplorer.net/#/gist/3a6d7dc99689e175f667747004a015f4/2c079662c137fb0f3af1be0f7c04920a9d6a199a

I see 3 solutions:

  1. Output generate code for the AST as import type {} from "some-module";
  2. Add AST properties so we know the difference between the 2
  3. Add support for import type "some-module"; to typescript (and flow script?)

Additional context

This got reported in StrykerJS: stryker-mutator/stryker-js#3386

We're using babel to parse and output files as is, after which tsc might need to run.

Metadata

Metadata

Labels

outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: generator

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions