microsoft / microsoft/TypeScript

@ts-ignore on an import that gets redefined strips the import from output

Open
#37,047 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: JS Emit
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.7.5 and 3.8.0

Search Terms:

  • ts-ignore remove import
  • ts-ignore strip import

Code

// @ts-ignore: Local Foo override
import Foo from "foo";

declare const Foo: {
    bar(): number;
}

Foo.bar();

Expected behavior:

JS output should probably be:

import Foo from "foo";

Foo.bar();

Actual behavior:

JS output is:

Foo.bar();

Playground Link: https://www.typescriptlang.org/play/index.html?ts=3.8-Beta&ssl=9&ssc=1&pln=1&pc=1#code/PTAEAEBcGcFoEsDmA7A9gJwKYC5QBlUBjAQwBtQAxVVUVAN03XXgBNMAoeAWwAcNJK1UADN0qLqABEw6pIDc7dm0KliWUIVTJoAqqlwBvdqBOgARmoAUASlzIArlzOMFAX0V6AdBfQ2FQA

Related Issues: None so far


Additional remarks: We are aware of other ways to type the foo module but in this particular case the foo override needs to be done in the file using it. As far as we tried, adding a ts-ignore is the only way to make ts understand that we want to redefine the module.
Now, this seems to move the declaration site from the import to the the declare statement and that has the effect to mark the import site as unused, since the import is unused, ts strips it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior from the provided TypeScript 3.7.5/3.8.0 code and Playground link, then trace the compiler's import-elision decision for an import followed by a redeclaration. Done means the emitted JavaScript retains the import as shown in the expected output, with coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.