Skip to content

import type should not have any effect on output code #41562

Closed
@KeithHenry

Description

@KeithHenry

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:

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