如何获得 Node 模块补全支持?

How can I get Node module completions support?

VS Code 似乎不知道 connectexpressio 是什么。我能告诉它吗?

是的,文档对此有说明。

You can get IntelliSense for many popular Node frameworks by incorporating TypeScript Definition files into your workspace. A TypeScript Definition (TSD) describes the API you are consuming in TypeScript, but it does not implement the API.

Visual Studio Code reads TSDs and in turn provides a rich IntelliSense experience based on the metadata provided by the definition. While you could use tsd to search for and install TypeScript definitions, VSCode provides a Quick Assist to automate the process.

Open app.js and notice that __dirname (lines 14, 23) have a green underline, indicating a warning. Click on __dirname and press Ctrl+. to open the Quick Fix context menu and choose Add /// reference to /node/node.d.ts.

按照此处的说明进行操作:https://code.visualstudio.com/Docs/nodejs

基本上,步骤是:

  1. 获取 TypeScript 定义文件并包含它
  2. 或者使用 VSCode
  3. 的帮助生成 TypeScript 类型

您必须下载 tsd 文件并在文件中使用 /// 语法引用它。

另一种选择是在服务器上使用 TypeScript。

这会变得更容易

您可以从 https://github.com/borisyankov/DefinitelyTyped or with the tsd-tool. For, let's say express, download https://raw.githubusercontent.com/borisyankov/DefinitelyTyped/master/express/express.d.ts 获取定义文件并将其添加到您的源文件:/// <reference path="myPathTo/express.d.ts" />

您很可能最终会得到一堆 d.ts 文件,因此您可以拥有一个元 d.ts 文件,该文件引用了所有这些文件并由您的来源引用。

/// <reference path="fileA.d.ts" />
/// <reference path="fileB.d.ts" />
/// <reference path="fileC.d.ts" />

并且在您的来源中执行此操作

/// <reference path="references.d.ts" />

"The TypeScript Definition Manager (TSD) makes it easy to search for and install TypeScript definition files into your Visyual Studio Code workspace. This tool will download the requested definition from the DefinitelyTyped repository."

通过 npm 安装 tsd:

npm install tsd -g

然后安装你需要的任何东西,所以拉下 Node 和 Express 定义。:

tsd query node express --action install

这是一个link to more info