Skip to content

ES Module imports discarded when they include a type and a side-effect #28285

Closed
@KeithHenry

Description

@KeithHenry

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 an export 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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions