使用 TypeScript 中的类型导入节点和表达 [英] Importing node and express with typings in TypeScript
问题描述
我正在尝试按照 Microsoft 指南 但是将其更改为使用 TypeScript 但是在使用 typings
安装类型定义时,我似乎必须安装比指南更多的包.
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.
我正在运行以下命令对:
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'.
对于以下类型:
- 哑剧
- express-serve-static-core
- 静态服务
我可以通过安装所需的类型来解决这个问题,但似乎类型本身应该做一些事情.
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?
如果相关,我的 tsconfig.json:
In case it's relevant, my 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.
My tsc is version 1.8.7 and I have typescript installed globally.
推荐答案
从上个月发布的 TypeScript 2.0 开始,推荐的安装 Typings 的工具是我们可信赖的老朋友 npm
而不是 typings
或 tsd
.
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
.
npm install @types/node --save
使用 npm,您无需再担心全局"或环境"安装.
With npm, there's no need to worry about "global" or "ambient" installations anymore.
您也不必再担心将
标签添加到源文件的顶部;只需将以下属性放入 tsconfig.json
中的 compilerOptions
中,TypeScript 编译器将自动找到您安装的 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:
"typeRoots": [ "node_modules/@types" ]
这里有一篇博文更详细地解释了这一变化:https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/
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/
这篇关于使用 TypeScript 中的类型导入节点和表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!