我应该如何在 TypeScript 2 中使用 @types

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38444279/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:13:58  来源:igfitidea点击:

How should I use @types with TypeScript 2

typescripttsdtypescript-typings

提问by gilamran

So far we are used to tsdor (The better version of it) typings

到目前为止,我们习惯于使用tsd或(更好的版本)打字

But now that TypeScript 2 offers the new @typesfeature, how should I convert my current project to work with @types?

但是现在 TypeScript 2 提供了新@types功能,我应该如何将我当前的项目转换为使用 @types?

I have tsd.json (typings.json is some cases) with all the dependencies, what are the steps to do the move to TypeScript 2?

我有 tsd.json(typings.json 是某些情况下)的所有依赖项,迁移到 TypeScript 2 的步骤是什么?

What are the newbest practices? Does @types support specific versions?

有哪些新的最佳实践?@types 是否支持特定版本?

回答by David Sherret

It's very simple. Just install the definitions that you need via npm.

这很简单。只需通过 npm 安装您需要的定义。

For example if you need lodash you can do:

例如,如果您需要 lodash,您可以执行以下操作:

npm install --save @types/lodash

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/@typesfolder by default. There's no need for a tsd.jsonor typings.jsonfile anymore.

安装后,您可以立即在您的项目中使用它。默认情况下,Typescript将从node_modules/@types文件夹中解析已安装的 @types 包的类型。不再需要tsd.jsonTypings.json文件。

Additional points:

附加点:

  • 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 typeRootsand typeshere. Specifically pay attention to these two points:
    • If typeRootsis 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 typesis specified in tsconfig.json, then only the packages specified will be included.
  • npm 中@types 包的主要和次要版本应与包版本相对应。
  • 您可以在此处搜索类型:http: //microsoft.github.io/TypeSearch/
  • 阅读关于typeRootstypes这里。具体要注意这两点:
    • 如果typeRootstsconfig.json中指定,则只有指定的文件夹将用于类型根。这将排除./npm_modules/@types/除非您指定它。
    • 如果typestsconfig.json中指定,则仅包含指定的包。

Read more in the blog post here.

此处的博客文章中阅读更多内容。

回答by Artru

Typescript 2.0gets rid of previous Typingssystem.
Now Typescript 2.0should by default look into ./node_modules/@typesand get types that you installed as the separate node modules, e.g. npm install --save @types/react(as mentioned by @David Sherret)

Typescript 2.0摆脱了以前的Typings系统。
现在Typescript 2.0应该默认查看./node_modules/@types并获取您作为单独节点模块安装的类型,例如npm install --save @types/react(如@David Sherret 所述)

There is a bug in the current version Typescript 2.0 beta, which does not load new types. Manually via cmd new tsc compiles files, but there is no IntelliSense support in VS 2015, and no errors are showed while a .ts file is in edit mode.

当前版本Typescript 2.0 beta 中存在一个错误,它不会加载新类型。手动通过 cmd new tsc 编译文件,但在 VS 2015 中没有 IntelliSense 支持,并且在 .ts 文件处于编辑模式时不会显示任何错误。

To resolve it modify tsconfig.jsonwith the similar settings:

要解决它修改tsconfig.json类似的设置:

{
  "compilerOptions": {
     // ... other config rows 
   "typeRoots": [ "node_modules/@types/" ],
    "types": [ "jquery", "react", "react-dom", /*... your other types */ ],
  }
}

For me manual "types"declaration helped resolved this issue, for other guys "typeRoots"helped. Hopefully it will save developer's hours.

对我来说,手动"types"声明帮助解决了这个问题,对于其他人"typeRoots"帮助了。希望它可以节省开发人员的时间。

回答by toskv

It looks like they are all just npm packages, you can find all the supported ones here.

看起来它们都只是 npm 包,你可以在这里找到所有支持的包。

tscwill pick up any types in the node_modulesfolder.

tsc将选取node_modules文件夹中的任何类型。

You can move the dependencies you have in typings.jsonin package.json(provided you change the names too ofcourse).

您可以在package.json 中移动typings.json中的依赖项(当然前提是您也更改了名称)。

You can read more about that here.

您可以在此处阅读更多相关信息。

回答by basarat

how should I convert my current project to work with @types

我应该如何将当前项目转换为使用 @types

I definitely recommend holding on for a bit longer.

我绝对建议再坚持一段时间。

e.g. issues are still getting fixed ... just 4 hours ago : https://github.com/Microsoft/TypeScript/issues/9725#issuecomment-233469422

例如问题仍在得到解决......就在 4 小时前:https: //github.com/Microsoft/TypeScript/issues/9725#issuecomment-233469422