Skip to content

error importing type from esm in commonjs #47338

Description

Bug Report

🔎 Search Terms

typeof import
import esm type from commonjs module
node12 nodenext

🕗 Version & Regression Information

  • I was unable to test this on prior versions because "module": "node12" only works in typescript@next

⏯ Playground Link

I was unable to find a way to emulate "type": "commonjs" in package.json on typescript playground

here is a repo that can be used to reproduce this error
https://github.com/nstringham/min-to-reproduce-typescript-import-problem

💻 Code

/tsconfig.json

{
  "compilerOptions": {
    "module": "nodenext"
  }
}

/dependency/index.d.ts

export type Color = "red" | "green" | "blue";

export function printColor(color: Color): void;

/index.ts

import type { Color } from "dependency";

const myColor: Color = "blue";

const myOtherColor: import("dependency").Color = "red";

async function main() {
  const { printColor } = await import("dependency");
  printColor(myColor);
  printColor(myOtherColor);
}

main();

🙁 Actual behavior

code compiles correctly but displays the following error

index.ts:1:28 - error TS1471: Module 'dependency' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.

1 import type { Color } from "dependency";
                             ~~~~~~~~~~~~

index.ts:5:28 - error TS1471: Module 'dependency' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.

5 const myOtherColor: import("dependency").Color = "red";
                             ~~~~~~~~~~~~

🙂 Expected behavior

code compiles correctly with no errors

↪️ Workaround

because typescript compiles correctly despite errors this error can be ignored with // @ts-ignore

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions