microsoft / microsoft/TypeScript

error TS2835 suggests `.js` extension when `rewriteRelativeImportExtensions` is enabled

Offen
#60,926 0 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 Search Terms
  • error TS2835
  • rewriteRelativeImportExtensions
  • allowImportingTsExtensions
  • tsconfig
  • diagnostics
🕗 Version & Regression Information
  • This changed between versions: N/A and 5.7.2
  • This changed in commit or PR: N/A
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about: rewriting extensions
  • I was unable to test this on prior versions because: the feature didn't exist yet
⏯ Playground Link

Playground does not support multiple files to demonstrate the diagnostic

💻 Code
//@filename src/foo.ts

export const foo = "foo"

//@filename src/bar.ts

import { foo } from './foo'

And in tsconfig:

// compiler options
    "moduleResolution": "nodenext",
    "allowImportingTsExtensions": true,
    "rewriteRelativeImportExtensions": true,

Though here is my full tsconfig just in case:

{
  "compilerOptions": {
    // Enable latest features
    "lib": [
      "ESNext",
      "DOM"
    ],
    "target": "ESNext",
    "module": "NodeNext",
    "moduleDetection": "force",
    "jsx": "react-jsx",
    "allowJs": false,

    "moduleResolution": "nodenext",
    "allowImportingTsExtensions": true,
    "rewriteRelativeImportExtensions": true,
    "verbatimModuleSyntax": true,
    "noEmit": true,
    // Best practices
    "strict": true,
    "skipLibCheck": true,
    "noFallthroughCasesInSwitch": true,
    // Some stricter flags (disabled by default)
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noPropertyAccessFromIndexSignature": false,
    // Awesome bundling
    "isolatedDeclarations": true,
    "composite": true
  }
}
🙁 Actual behavior
src/bar.ts:1:21 - error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './foo.js'?

1 import { foo } from "./foo";

The diagnostic suggests a file extension ending in .js, even when both allowImportingTsExtensions and rewriteRelativeImportExtensions are enabled.

🙂 Expected behavior
src/bar.ts:1:21 - error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './foo.ts'?

1 import { foo } from "./foo";

The suggested import path should probably be .ts when these options are enabled.

Assuming I had rewriteRelativeImportExtensions: false, and given my above settings which is not using bundler mode, it would make sense to suggest .js. However, because rewrite is enabled, I think the best option would be to suggest .ts.

It also seems like tsserver should be able to offer an intellisense fix here in general, but I'm not sure, and that's a bit unrelated.

Additional information about the issue

Additionally, the wiki faq seems slightly outdated now with the addition of rewriteRelativeImportExtensions. It currently says the following:

### Module Specifier Rewriting
It's explicitly out of scope for TypeScript to modify module specifiers as they appear in emitted JS, e.g [...]

[...] This includes things like changing file extensions

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den TS2835-Diagnostic nachzuverfolgen und zu untersuchen, wie moduleResolution, allowImportingTsExtensions und rewriteRelativeImportExtensions den vorgeschlagenen Pfad beeinflussen. Reproduziere anschließend das Beispiel mit den zwei Dateien src/foo.ts und src/bar.ts und der bereitgestellten tsconfig und überprüfe dann, dass der Diagnostic bei aktiviertem Umschreiben .ts vorschlägt, während andernfalls der bestehende .js-Vorschlag erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.