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:

Code

{ "compilerOptions": { "target": "esnext", "module": "esnext" } }

export class SourceComponent extends HTMLElement { // Web component implementation
} customElements.define('source-component', SourceComponent);

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;