node.js - VsCode 干净的 Typescript 项目没有自动导入建议

标签 node.js typescript visual-studio-code

当我使用 Typescript 创建一个新的 Node 项目时,VsCode 自动导入建议根本不起作用。

重现步骤:

创建工作区目录。

运行 npm init 指定 main.ts 作为入口文件。

导入 typescript cli npm i typescript

创建 tsconfig.json .\node_modules\.bin\tsc --init

创建包含 console.log('Running');

的 main.ts

使用 .\node_modules\.bin\tsc -w 转译

通过在 VsCode 中单击 Debug 并使用默认的 Nodejs 启动配置来运行。

导入库,例如Rxjs 导入 typescript cli npm i rxjs

结果:

在 main.ts 中尝试使用任何 Rxjs 类型 ObservableBehaviourSubject、全局运算符 from of sequenceEqual 等不会导致任何导入帮助。

我已经阅读了 VsCode Typescript 文档,没有提示那里有什么问题。 https://code.visualstudio.com/docs/languages/typescript

我已经尝试在 tsconfig.json 中显式设置 includeexclude 目录,但也没有效果。

我需要在 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');

的 index.ts

使用 .\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/

相关文章:

javascript - 返回 Unirest 客户端的 promise

javascript - 等待 Javascript 网页抓取功能完成后再运行下一页?

javascript - 如何在 TypeScript 中访问 jQuery .each 的 "this"?

visual-studio-code - 如何配置 VSCode 以在外部编辑器中打开给定的文件类型?

java - VSCode : disable Java linting

node.js - 仅使用 Express 框架对某些 URL 使用 HTTP 基本身份验证

javascript - Mongoose 方法返回未定义

typescript - 属性 'use' 在类型 'typeof..., Property ' 上不存在扩展'在类型'typeof 上不存在

typescript 不检测方法改变的属性

php - 使用虚拟机在端口 9000 上进行 XDebug - EADDRINUSE:::9000