Skip to content

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

Closed
@otakustay

Description

@otakustay

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 = {
  // ...
};

Metadata

Metadata

Assignees

Labels

CommittedThe team has roadmapped this issueFix AvailableA PR has been opened for this issuefeature-requestA request for a new feature

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions