ES Module imports discarded when they include a type and a side-effect · Issue #28285 · microsoft/TypeScript (original) (raw)
When an ES module syntax import has an explicit type and a side effect it is excluded from the output JS.
TypeScript Version: 3.2.0-dev.20181101
Search Terms:
- ES Modules
- Module resolution
Code
tsconfig.json
target ESNext
{ "compilerOptions": { "target": "esnext", "module": "esnext" } }
source-component.ts
with anexport
and a side effect
export class SourceComponent extends HTMLElement {
// Web component implementation
}
customElements.define('source-component', SourceComponent);
missing-import.ts
that references the dependency as a type and expects the side effect
import {SourceComponent} from './source-component.js'; const example = document.createElement('source-component') as SourceComponent;
Expected behaviour:
When output to missing-import.js
the reference should be retained:
import {SourceComponent} from './source-component.js'; const example = document.createElement('source-component');
An import as a side effect would be acceptable too:
import './source-component.js'; const example = document.createElement('source-component');
Actual behaviour:
The import
is stripped out:
const example = document.createElement('source-component');
Workaround:
Duplicate side effect import
statements are retained:
import {SourceComponent} from './source-component.js'; import './source-component.js'; const example = document.createElement('source-component') as SourceComponent;