Description
import type
declarations should not appear in the JS output and should not have any effect on the JS output.
As of 4.0 adding import type
to a file causes export {};
to be added to the output. This worked in 3.8
This breaks components that use ES modules syntax (because the rest of project also uses it) but will not be loaded as modules. Examples include service workers, web workers, shared workers and custom elements loaded as side effects.
This is a breaking change with no workaround possible in TS. External workarounds are possible.
The general case has already been raised as #41513, this issue is specifically for the subset where the files only contain import type
declarations and no plain import
statements.
The documentation for import type
states:
import type
only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there’s no remnant of it at runtime.
This was the case in 3.8, it is not the case with 4.0
TypeScript Version: 4.0.5
Search Terms: import type
, export {}
Code
Using import type
in a file so that TS can check types:
import type { InterfaceType } from './MyLibrary';
const test: InterfaceType = {};
Note that this file is not a module and will not be loaded as a module.
Expected behavior:
JS output should not include any reference to the import type
declaration:
const test = {};
Actual behavior:
JS output includes export {};
to force the output to be a module:
const test = {};
export {};
Related Issues:
- Bug in Webpack this change was introduced to fix: Empty Module Exports Dramatically Affect Bundle Output webpack/webpack#10889
- Not all TS users also use Webpack, is it a pre-requisite now?
- Design meeting where problems this change introduces were dismissed: Design Meeting Notes, 5/21/2020 #38713
- Apparently "They will have a point", though it's not clear whether anything followed from that.
- Change to always
export {};
where this exact issue withimport type
was already identified: Consider emittingexport {}
in all ambiguous module output #38696 (comment) - Bug raised for general case: TypeScript adds
exports {}
into code #41513