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

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

Closed
trusktr opened this issue Jun 5, 2023 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@trusktr
Copy link
Contributor

trusktr commented Jun 5, 2023

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.

@trusktr
Copy link
Contributor Author

trusktr commented Jun 5, 2023

A simple workaround for now is to put a // @ts-expect-error comment over the import:

// @ts-expect-error moduleResolution:nodenext issue 54523
import type {SomeType} from 'some-lib'

@fatcerberus
Copy link

Known issue, see #52529. @ts-ignore/@ts-expect-error is the only current workaround.

@bmeck
Copy link

bmeck commented Jun 5, 2023

Seeing this as well after updating TS, workaround doesn't work as the type is still seen as non-existent.

@andrewbranch
Copy link
Member

I think you can add the resolution-mode import assertion and ts-ignore the error that tells you not to do that.

import type { SomeType } from 'some-lib' assert { "resolution-mode": "module" }

This will become an import attribute and not illegal once we land support for import attributes.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 14, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@TheNoim
Copy link

TheNoim commented Aug 26, 2024

import type { SomeType } from 'some-lib' assert { "resolution-mode": "module" }

This is currently the correct way I guess:

import type { SomeType } from 'some-lib' with { 'resolution-mode': 'import' };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

7 participants