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

Allow synchronous type-only import of ESM package from CJS #47248

Closed
5 tasks done
otakustay opened this issue Dec 25, 2021 · 2 comments · Fixed by #47807
Closed
5 tasks done

Allow synchronous type-only import of ESM package from CJS #47248

otakustay opened this issue Dec 25, 2021 · 2 comments · Fixed by #47807
Assignees
Labels
Committed The team has roadmapped this issue feature-request A request for a new feature Fix Available A PR has been opened for this issue

Comments

@otakustay
Copy link

Suggestion

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

import, type, esm

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

📃 Motivating Example

For a ESM-only package esm-package, if we import types from it in a CommonJS file:

import type {SomeType} from 'esm-package';

TypeScript warns in typescript 4.6.0-dev.20211225:

Module 'esm-package' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.ts(1471)

For type-only import this shouldn't matter since it doesn't emit JavaScript code, so we can allow

💻 Use Cases

Types, in some cases, are not possible to lazy import asynchronously, such as a top level export:

import type {SomeType} from 'esm-package';

export const DEFAULT_CONFIG: SomeType = {
  // ...
};
@fatcerberus
Copy link

fatcerberus commented Dec 25, 2021

That error makes no sense. It’s an import, not a require() - why wouldn’t it be able to load ES modules using the ES module import syntax? 😕

That said, I’d expect this to work:

type SomeType = import(‘esm-package').SomeType;

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jan 11, 2022
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.6.0 milestone Jan 11, 2022
@weswigham weswigham added Committed The team has roadmapped this issue feature-request A request for a new feature Fix Available A PR has been opened for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Feb 10, 2022
@weswigham
Copy link
Member

#47807 should provide options here - you'll be able to explicitly

type SomeType = import(‘esm-package', { assert: {"resolution-mode": "import"} }).SomeType;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Committed The team has roadmapped this issue feature-request A request for a new feature Fix Available A PR has been opened for this issue
Projects
None yet
4 participants