使用 TypeScript 中的类型导入节点和表达
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36013174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing node and express with typings in TypeScript
提问by Underscore
I am trying to set up a TypeScript express/node application in Visual Studio Code following the Microsoft guidebut 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.
我正在尝试按照Microsoft 指南在 Visual Studio Code 中设置 TypeScript express/node 应用程序,但将其更改为使用 TypeScript 但是在使用安装类型定义时,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 版,并且在全球范围内安装了 typescript。
回答by Josh1billion
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
.
从上个月发布的 TypeScript 2.0 开始,推荐的安装类型的工具是我们值得信赖的老朋友,npm
而不是typings
或tsd
。
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; just drop the following property into your compilerOptions
in tsconfig.json
, and the TypeScript compiler will automatically find the npm typings you've installed:
您也无需再担心<reference>
在源文件顶部添加标签;只需将以下属性放入您的compilerOptions
in 中tsconfig.json
,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/
回答by Underscore
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 commentfor information about why dependencies are not automatically downloaded.
有关为什么不自动下载依赖项的信息,请参阅 @cdbajorin 的评论。
回答by Will Munn
to save everyone a headache, the magic command to get the typings for node is now:
为了让大家省心,获取节点类型的魔法命令现在是:
typings install node --source env --global --save
typings install node --source env --global --save