簡體   English   中英

在.js 文件中導入.ts 文件時缺少智能感知

[英]Missing intellisense when importing .ts file in .js file

我嘗試使用tsx運行由 JavaScript 和 Typescript 模塊組成的 NodeJS 應用程序。 JavaScript 模塊是 ESM,而不是 CommonJS。 例如我有這些文件:

// provider.ts
export funcA(p: unknown): unknown {...}

// consumer.js
import * as provider from "./provider.ts"

provider.funcA("foo");

它與 tsx 一起運行得很好。 VSCode 的智能感知在 TS 文件中運行良好,但在 JS 文件中卻不行。 當我 hover provider顯示import provider時,我沒有任何完成。 這是我的tsconfig.json

{
  "compilerOptions": {
    "module": "NodeNext",
    "target": "ESNext",
    "allowJs": true,
    "strict": true,
  },
  "include": [
    <the directory containing both JS and TS files>
  ],
}

如何讓 Intellisense 在 VSCode 中工作,以便在我的 JS 文件中導入 TS?

我找到了解決方案。 當我導入.ts模塊時,似乎 TypeScript 無法理解。

我不得不刪除導入的.ts部分。 我必須在我的tsconfig.json中添加"moduleResolution": "Node"以消除解析錯誤。

我的 eslint 配置也遇到了一些問題。 我安裝了包@typescript-eslint/parsereslint-import-resolver-typescript並將其添加到我的.eslintrc.json

"extends": [
  ...
  "plugin:import/typescript"
],
...
"settings": {
  "node": {
    "tryExtensions": [".js", ".node", ".ts"]
  }
}

現在它起作用了。 我在我的 JS 文件中獲得了完整的 Intellisense。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM