问题描述
到目前为止,我们习惯于 tsd 或(更好的版本)打字
So far we are used to tsd or (The better version of it) typings
但是现在 TypeScript 2 提供了新的 @types
功能,我应该如何将我当前的项目转换为使用 @types?
But now that TypeScript 2 offers the new @types
feature, how should I convert my current project to work with @types?
我有 tsd.json(在某些情况下为typings.json)以及所有依赖项,迁移到 TypeScript 2 的步骤是什么?
I have tsd.json (typings.json is some cases) with all the dependencies, what are the steps to do the move to TypeScript 2?
什么是新最佳实践?@types 是否支持特定版本?
What are the new best practices?Does @types support specific versions?
推荐答案
很简单.只需通过 npm 安装您需要的定义.
It's very simple. Just install the definitions that you need via npm.
例如,如果您需要 lodash,您可以这样做:
For example if you need lodash you can do:
npm install --save @types/lodash
安装后,您可以立即在您的项目中使用它.默认情况下,Typescript 将从 node_modules/@types 文件夹中解析已安装的 @types 包的类型.不再需要 tsd.json 或 typings.json 文件.
Once it's installed you can use it right away in your project. Typescript will resolve the typings for the installed @types package from the node_modules/@types folder by default. There's no need for a tsd.json or typings.json file anymore.
附加点:
- npm 中@types 包的主要和次要版本应与包版本相对应.
- 您可以在此处搜索类型:http://microsoft.github.io/TypeSearch/
- 阅读
typeRoots
和types
在这里.具体要注意这两点:- 如果在 tsconfig.json 中指定了
typeRoots
,则只有指定的文件夹将用于类型根.这将排除 ./npm_modules/@types/ 除非你指定它. - 如果在 tsconfig.json 中指定了
types
,那么只会包含指定的包.
- The major and minor version of the @types package in npm should correspond with the package version.
- You can search for types here: http://microsoft.github.io/TypeSearch/
- Read about
typeRoots
andtypes
here. Specifically pay attention to these two points:- If
typeRoots
is specified in tsconfig.json, then only specified folders will be used for the type roots. That will exclude ./npm_modules/@types/ unless you specify it. - If
types
is specified in tsconfig.json, then only the packages specified will be included.
在博客文章中阅读更多内容 这里.
Read more in the blog post here.
这篇关于我应该如何在 TypeScript 2 中使用 @types的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- If
- 如果在 tsconfig.json 中指定了