[英]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/parser
和eslint-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.