microsoft / microsoft/TypeScript
error TS2835 suggests `.js` extension when `rewriteRelativeImportExtensions` is enabled
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず TS2835 診断を追跡し、moduleResolution、allowImportingTsExtensions、rewriteRelativeImportExtensions が提案されるパスにどのような影響を与えるかを確認します。提供された tsconfig を使って 2 ファイルの src/foo.ts と src/bar.ts の例を再現し、書き換えが有効な場合は診断が .ts を提案し、それ以外の場合は既存の .js の提案が維持されることを検証します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100