在TypeScript中导入节点并用types表示

我试图在Visual Studio代码中按照Microsoft指南设置TypeScript express / node应用程序,但将其更改为使用TypeScript,但是当涉及到使用types安装types定义时,我似乎必须安装比指南更多的包。

我正在运行以下一对命令:

 typings install node --ambient --save typings install express --ambient --save 

但是试图用这些软件包进行构build会导致以下types的错误:

 error TS2307: Cannot find module 'serve-static'. 

对于以下types:

  • 哑剧
  • 快车服务静态核心
  • 服务静电

我可以通过安装所需的types来解决这个问题,但是它似乎应该自行完成。

我想检查一下是否缺less自动引入依赖关系的基本步骤,或者引导是否过时?

如果它是相关的,我的tsconfig.json:

 { "compilerOptions": { "target": "es5", "module": "commonjs", "sourceMap": true, "outDir": "bin", "sourceRoot": "src" }, "exclude": [ "node_modules", "typings/browser.d.ts", "typings/browser" ] } 

我的tsc版本是1.8.7,我在全球安装了打字机。

从上个月发布的TypeScript 2.0开始,安装types的推荐工具是我们可靠的老朋友npm而不是types或tsd

 npm install @types/node --save 

有了npm,就不用担心“全局”或“环境”的安装了。

您也不必担心将<reference>标签添加到源文件的顶部; 只需将以下属性放入tsconfig.jsoncompilerOptions中,TypeScript编译器将自动查找已安装的npmtypes:

 "typeRoots": [ "node_modules/@types" ] 

以下是一篇博文,详细解释了这一变化: https : //blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/

我链接的教程现在已经更新,包括以下命令:

 typings install node --ambient typings install express serve-static express-serve-static-core --ambient 

请参阅@cdbajorin的评论 ,了解为什么不自动下载依赖关系的信息。

为了节省大家的头痛,现在得到节点打字的魔法指令是:

typings install node --source env --global --save