Skip to content

Should type-only import resolving to ES module from CJS module be allowed? #52529

@ShogunPanda

Description

@ShogunPanda

Bug Report

🕗 Version & Regression Information

I observed this in TypeScript 4.9.x, couldn't test earlier versions.

💻 Code

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "target": "ESNext",
    "module": "CommonJS",
    "outDir": "dist",
    "lib": ["ESNext", "DOM"],
    "esModuleInterop": false,
    "declaration": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "moduleResolution": "nodenext",
    "baseUrl": "."
  },
  "include": ["index.ts"]
}

package.json:

{
	"dependencies": {
		"got": "^12.5.3",
		"typescript": "^4.9.5"
	}
}

index.ts:

import type { Headers } from 'got'

const foo: Headers = {}
console.log(foo)

🙁 Actual behavior

Error reported:

index.ts:1:30 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("got")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/Volumes/DATI/Users/Shogun/Programmazione/Test/got/package.json'.

1 import type { Headers } from 'got'
                               ~~~~~


Found 1 error in index.ts:1

Note the got is just a quick repro example. It happening with any ESM module.

🙂 Expected behavior

Since I'm just doing import type, the emitted file has no requires at all and thus the error is unnecessary.

Metadata

Metadata

Assignees

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