Skip to content

importsNotUsedAsValues in JS files elides imports incorrectly #38412

Closed

Description

TypeScript Version: 4.0.0-dev.20200508

Search Terms:
importsNotUsedAsValues
import eliding

Code

In TypeScript 3.8, a new importsNotUsedAsValues feature/compilerOption was added. This, however is producing different output based on if the file is .ts or .js.

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "noResolve": true,
    "importsNotUsedAsValues": "preserve",
    "target": "ES2015",
    "outDir": "./out",
    "types": []
  }
}

test.js

// @ts-nocheck
import * as moment from 'moment';
import rollupMoment__default from 'moment';
export const moment = rollupMoment__default || moment;

Output

// @ts-nocheck
import * as moment from 'moment';
import 'moment';
export const moment = rollupMoment__default || moment;

Removing the importsNotUsedAsValues option, which is equivalent of having the value of remove will produce:

// @ts-nocheck
import * as moment from 'moment';
export const moment = rollupMoment__default || moment;

Changing the file extension from .js to .ts will produce the correct output for both cases

// @ts-nocheck
import * as moment from 'moment';
import rollupMoment__default from 'moment';
export const moment = rollupMoment__default || moment;

Expected behavior:

  • Used imports are not elided.
  • Given the same input it should produce the same output, irrespective of the file extension.

Actual behavior:

  • Given a JS input the import is elided incorrectly.

Playground Link: Playground doesn't have the importsNotUsedAsValues options.
Related Issues: None that I can find.

Activity

  1. alan-agius4 commented on May 8, 2020

    @alan-agius4
    ContributorAuthor

    Note: this bug is also present in 3.8 & 3.9.

  2. added
    BugA bug in TypeScript
    Fix AvailableA PR has been opened for this issue
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Jun 9, 2020
  3. milahu commented on Apr 15, 2021

    @milahu

    Playground Link: Playground doesn't have the importsNotUsedAsValues options.

    https://codesandbox.io/s/typescript-importsnotusedasvalues-preserve-bug-demo-qm23x

  4. andrewbranch commented on Apr 15, 2021

    @andrewbranch
    Member

    milahu would you mind opening a new issue?

  5. milahu commented on Apr 15, 2021

    @milahu

    #43693 (sorry for the noise)

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions