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