当我使用 Typescript 创建一个新的 Node 项目时,VsCode 自动导入建议根本不起作用。
重现步骤:
创建工作区目录。
运行 npm init
指定 main.ts
作为入口文件。
导入 typescript cli npm i typescript
创建 tsconfig.json .\node_modules\.bin\tsc --init
创建包含 console.log('Running');
使用 .\node_modules\.bin\tsc -w
转译
通过在 VsCode 中单击 Debug
并使用默认的 Nodejs 启动配置来运行。
导入库,例如Rxjs 导入 typescript cli npm i rxjs
结果:
在 main.ts 中尝试使用任何 Rxjs 类型 Observable
、BehaviourSubject
、全局运算符 from
of
sequenceEqual
等不会导致任何导入帮助。
我已经阅读了 VsCode Typescript 文档,没有提示那里有什么问题。 https://code.visualstudio.com/docs/languages/typescript
我已经尝试在 tsconfig.json 中显式设置 include
和 exclude
目录,但也没有效果。
我需要在 tsconfig 中手动设置一些模块解析选项吗?
我不知道,我不知道为什么这不起作用。
更新:
我设法通过指定让 VsCode 自动导入工作
"typeRoots": [ "node_modules" ]
...在 tsconfig.json 中
导致一个小问题,即 tsc 现在报告错误
error TS2688: Cannot find type definition file for '.bin'.
看起来 tsc 现在尝试读取 node_modules.bin 的内容,但没有帮助。我试过设置
"exclude": [ "./node_modules/.bin"]
在 tsconfig 中,但这没有任何效果。
最佳答案
尝试将 main.js
作为入口点而不是 main.ts
我遵循的步骤:
创建工作区目录。
运行 npm init 指定 index.js
作为入口点文件。
安装 typescript cli:npm i typescript
创建 tsconfig.json:.\node_modules\.bin\tsc --init
创建包含 console.log('Running');
使用 .\node_modules\.bin\tsc -w
转译
通过在 VSCode 中单击“调试”并使用默认的 Nodejs 启动配置来运行。
安装一个库,例如Rxjs:npm i rxjs
现在在 index.ts 文件中:import { observable, BehaviorSubject } from "rxjs"
;
我能够获得那些 IntelliSense(导入帮助)
关于node.js - VsCode 干净的 Typescript 项目没有自动导入建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52493422/