Skip to content

TS1479 Unable to import types from ESM package when moduleResolution is nodenext #54523

@trusktr

Description

@trusktr

Bug Report

When I set moduleResolution: "nodenext" I see this error when I try to import type from a package:

Screenshot 2023-06-04 at 11 33 29 PM

Then I tried to switch to dynamic import() (even in for a type, not a runtime value), and get the same error:

Screenshot 2023-06-04 at 11 30 19 PM

With runtime value, we can typically switch to dynamic import() to get around the problem. But with type imports, this is not getting around the problem.

Here's the error message for searchability:

error message (click to open)
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("wazum")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/Users/trusktr/src/project/package.json'.ts(1479)

Note that the error says

Consider writing a dynamic 'import("wazum")' call instead.

which is what I've done with import('wazum').Add.

Adding typeof has the same issue:

Screenshot 2023-06-04 at 11 39 25 PM

Note, the wazum package is not the one currently at https://npmjs.com/wazum, but the one installed from my current GitHub fork:

npm install 'wazum@trusktr/wazum#simple-esm-dist-output'

🔎 Search Terms

typescript moduleResolution nodenext unable to import types

🕗 Version & Regression Information

Not sure when it regressed, if at all.

⏯ Playground Link

I am unable to provide a playground link because it doesn't seem to be configurable with the required options.

TODO: make a clonable reproduction.

💻 Code

type t = import('wazum').Add // error ts(1479)

Here's my tsconfig:

{
	"compilerOptions": {
		// Compiler setup
		"target": "esnext",
		"module": "commonjs",
		"lib": ["ESNext"],
		"sourceMap": true,
		"outDir": "out",
		"moduleResolution": "nodenext",
		"esModuleInterop": true,
		"skipLibCheck": true,
		"forceConsistentCasingInFileNames": true,

		// Type checking
		"strict": true,
		"noUnusedLocals": true,
		"noImplicitReturns": true,
		"noImplicitOverride": true
	},
	"include": ["src/**/*.ts"],
	"exclude": ["out", "node_modules"]
}

🙁 Actual behavior

Unable to import types.

🙂 Expected behavior

Expect there to be some way to import types. import type imports are not regular ES Module imports, so it seems like import type {...} from '...' should just work, however I understand if it should match with the runtime import() requirement for CommonJS output for consistency, but that's just not working either.

Plus using dynamic import() syntax for type imports is too cumbersome because some features are missing, f.e. the ability to import generic types:

Screenshot 2023-06-04 at 11 56 31 PM

So even if import() is what I should be doing (currently not working) import type would be better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions