npm @types org 包中的 TypeScript 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37548066/
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
TypeScript typings in npm @types org packages
提问by Ronald Zarīts
I've noticed there's an npm organization @types, which contains typing packages, but can't find any documentation on it. How are these meant to be used?
我注意到有一个 npm 组织@types,其中包含打字包,但找不到任何关于它的文档。这些是如何使用的?
Is it meant to be used with typingstool? If so, how to install them? For instance, there's a @types/openlayers
package, but typings search npm:openlayers
returns nothing.
它是否意味着与打字工具一起使用?如果是这样,如何安装它们?例如,有一个@types/openlayers
包,但typings search npm:openlayers
什么都不返回。
Is it meant to be used separately from the typings tool? E.g. installed directly with npm
?
它是否意味着与打字工具分开使用?例如直接安装npm
?
采纳答案by randominstanceOfLivingThing
As of TypeScript 2.0, typingsis no longer required. The npm organization is an entity to setup a developers team. I believe Microsoft setup the @types organization in npm and added the TypeScript developer team to the organization. Packages on under the @types organization are published automatically from DefinitelyTyped using the types-publisher tool as per the docs.
从 TypeScript 2.0 开始,不再需要打字。npm 组织是建立开发团队的实体。我相信微软在 npm 中设置了 @types 组织,并将 TypeScript 开发人员团队添加到该组织中。@types 组织下的包是根据docs使用 types-publisher 工具从绝对类型自动发布的。
In addition, to there is another way to add types
to your packages:
此外,还有另一种方法可以添加types
到您的包中:
In your package.json
在你的 package.json 中
If your package has a main .js
file, you will need to indicate the main declaration file in your package.json
file as well. Set the types
property to point to your bundled declaration file. For example:
如果你的包有一个主.js
文件,你还需要在你的package.json
文件中指明主声明文件。将该types
属性设置为指向您的捆绑声明文件。例如:
{
"name": "awesome",
"author": "Vandelay Industries",
"version": "1.0.0",
"main": "./lib/main.js",
"types": "./lib/main.d.ts"
}
Note that the "typings"
field is synonymous with "types"
, and could be used as well.
请注意,该"typings"
字段与 同义"types"
,也可以使用。
Also note that if your main declaration file is named index.d.ts
and lives at the root of the package (next to index.js
) you do not need to mark the "types"
property, though it is advisable to do so.
另请注意,如果您的主声明文件已命名index.d.ts
并位于包的根目录(在 旁边index.js
),则您不需要标记该"types"
属性,但建议这样做。
Regarding searching types
关于搜索类型
For the most part, type declaration packages should always have the same name as the package name on npm, but prefixed with @types/, but if you need, you can check out https://aka.ms/typesto find the package for your favorite library.
在大多数情况下,类型声明包应该始终与 npm 上的包名同名,但以@types/ 为前缀,但如果需要,您可以查看https://aka.ms/types以查找包为您最喜爱的图书馆。
From - http://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html
来自 - http://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html
But when I did npm search @types/openlayers
, I did not get any results. But doing the search from the web interface did return me the results. So I guess npm search
does not search across organizations.
但是当我这样做时npm search @types/openlayers
,我没有得到任何结果。但是从 Web 界面进行搜索确实返回了结果。所以我猜npm search
不会跨组织搜索。
回答by Ronald Zarīts
Announcement on the TypeScript blog answers this: The Future of Declaration Files
TypeScript 博客上的公告回答了这个问题:声明文件的未来
Summary:
概括:
The @types
npm organization is for obtaining type definitions with npm
. Using these type definitions is a feature is coming in TypeScript 2.0.
该@types
NPM组织是获得与类型定义npm
。使用这些类型定义是 TypeScript 2.0 中的一项功能。
This will replace the current projects/tools such as typingsand tsd, though these will continue to be supported for some time.
回答by Sean Larkin
This is going to be a feature that is rolled out in Typescript 2.0. This provides type support for UMD Modules/Libraries and their respective definitions.
这将是 Typescript 2.0 中推出的一项功能。这为 UMD 模块/库及其各自的定义提供了类型支持。
See (Built-in support for UMD module definitions) to get a better understanding of the issues currently with ambient typings.
请参阅(对 UMD 模块定义的内置支持)以更好地了解当前环境类型的问题。