在TypeScript中导入节点并使用typing表达

Und*_*ore 23 node.js typescript visual-studio-code

我试图在Microsoft指南之后在Visual Studio代码中设置TypeScript表达/节点应用程序,但是将其更改为使用TypeScript,但是在使用typings我安装类型定义时,我似乎必须安装比指南更多的包.

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

typings install node --ambient --save
typings install express --ambient --save
Run Code Online (Sandbox Code Playgroud)

但是,尝试使用这些包进行构建会产生以下类型的错误:

error TS2307: Cannot find module 'serve-static'.
Run Code Online (Sandbox Code Playgroud)

对于以下类型:

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

我可以通过安装所需的打字来解决这个问题,但似乎某些打字应该自己做.

我想检查一下我是否错过了自动引入依赖关系或者指南是否过时的基本步骤?

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

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "sourceMap": true,
        "outDir": "bin",
        "sourceRoot": "src"
    },
    "exclude": [
        "node_modules",
        "typings/browser.d.ts",
        "typings/browser"
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的tsc是1.8.7版本,我在全球范围内安装了打字稿.

Jos*_*ion 31

自上个月发布TypeScript 2.0以来,安装打字的推荐工具是我们值得信赖的老朋友npm而不是typingstsd.

npm install @types/node --save
Run Code Online (Sandbox Code Playgroud)

使用npm,不再需要担心"全局"或"环境"安装.

您也不必担心<reference>在源文件的顶部添加标记了; 只需将以下属性放入您的compilerOptionsin中tsconfig.json,TypeScript编译器将自动找到您安装的npm类型:

"typeRoots": [ "node_modules/@types" ]
Run Code Online (Sandbox Code Playgroud)

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

  • `@sages`modules被定义为typescript 2中typedeclarations的默认值.因此`"typeroots":["node_modules/@ types`]`是不必要的,只要它是唯一的输入文件夹. (4认同)

Und*_*ore 8

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

typings install node --ambient
typings install express serve-static express-serve-static-core --ambient
Run Code Online (Sandbox Code Playgroud)

有关为什么不自动下载依赖项的信息,请参阅@cdbajorin的注释.

  • _-- ambient_已在[typings-1.0.0]中重命名为_-- global_(https://github.com/typings/typings/releases/tag/v1.0.0) (8认同)
  • 另外(据我所知)安装的默认存储库现在是npm.添加了一个名为env的新存储库,其中包含环境(例如节点)的类型声明.我用过:`typings install env~node@4.0.0 --save --global`不确定依赖关系的全局依赖关系的情况是否已经改变了. (2认同)