简体   繁体   English

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

[英]Importing node and express with typings in TypeScript

I am trying to set up a TypeScript express/node application in Visual Studio Code following the Microsoft guide but changing it to use TypeScript however when it comes to installing the type definitions using typings I seem to have to install more packages than the guide. 我试图设置以下在Visual Studio代码一个打字稿快递/节点应用微软引导 ,但改变它然而,当涉及到安装使用类型定义使用打字稿typings我似乎有比引导安装更多的包。

I'm running the following pair of commands: 我正在运行以下一对命令:

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

However attempting to build with just those packages gives the following type of error: 但是,尝试使用这些包进行构建会产生以下类型的错误:

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

For the following typings: 对于以下类型:

  • mime 哑剧
  • express-serve-static-core 快车服务静态核心
  • serve-static 服务静电

I can resolve this by installing the required typings but it seems like something typings should do by itself. 我可以通过安装所需的打字来解决这个问题,但似乎某些打字本身应该做。

I wanted to check if I was missing a fundamental step to automatically pull in dependencies or whether the guide was outdated? 我想检查一下我是否错过了自动引入依赖关系或者指南是否过时的基本步骤?

In case it's relevant, my tsconfig.json: 如果它是相关的,我的tsconfig.json:

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

My tsc is version 1.8.7 and I have typescript installed globally. 我的tsc是版本1.8.7,我在全球安装了打字稿。

As of the release of TypeScript 2.0 last month, the recommended tool for installing typings is our trusty old friend npm instead of typings or tsd . 作为打字稿2.0上个月发布的,用于安装分型的推荐工具是我们值得信赖的老朋友npm ,而不是typingstsd

npm install @types/node --save

With npm, there's no need to worry about "global" or "ambient" installations anymore. 使用npm,不再需要担心“全局”或“环境”安装。

You also don't need to worry about adding <reference> tags to the top of your source files anymore; 您也不必担心将<reference>标记添加到源文件的顶部; just drop the following property into your compilerOptions in tsconfig.json , and the TypeScript compiler will automatically find the npm typings you've installed: 只需将以下属性拖放到tsconfig.jsoncompilerOptions中,TypeScript编译器将自动找到您安装的npm类型:

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

Here's a blog post that explains the change in some more detail: https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/ 这是一篇博文,更详细地解释了这一变化: https//blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/

The tutorial I linked has now been updated to include the following commands: 我链接的教程现已更新,包括以下命令:

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

See @cdbajorin 's comment for information about why dependencies are not automatically downloaded. 有关为什么不自动下载依赖项的信息,请参阅@cdbajorin的注释

为了让每个人都头疼,现在获得节点输入的神奇命令是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM