microsoft / microsoft/TypeScript

"Import assignment cannot be used when targeting ECMAScript modules." should come with a codefix

Open
#28,241 0 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: LS: Quick Fixes In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.2.0-dev.20181030

Code

a.ts

import abs = require("abs");

tsconfig.json

{
    "compilerOptions": {
        "target": "esnext"
    }
}

The error message is Import assignment cannot be used when targeting ECMAScript modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.. This indicates that we could provide a code fix.

Expected behavior:

There is a codefix that offers to change this to import abs from "abs";, or import * as abs from "abs";. If --esModuleInterop is set and the imported value is not used only as a namespace, we should prefer a default import.

Actual behavior:

No codefix.

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 diagnostic with a.ts and the shown tsconfig.json, then trace the compiler's codefix handling for this error. The fix should offer default and namespace import forms, preferring a default import when esModuleInterop is enabled and the value is not used only as a namespace; verify both suggested edits and their applicability.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.