简体   繁体   English

npm @types org包中的TypeScript类型

[英]TypeScript typings in npm @types org packages

I've noticed there's an npm organization @types , which contains typing packages, but can't find any documentation on it. 我注意到有一个npm组织@types ,它包含输入包,但找不到任何文档。 How are these meant to be used? 这些意味着如何使用?

Is it meant to be used with typings tool? 它是否意味着与打字工具一起使用? 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? 它是否意味着与打字工具分开使用? Eg installed directly with npm ? 例如直接用npm安装?

As of TypeScript 2.0, typings is no longer required. 从TypeScript 2.0开始,不再需要打字 The npm organization is an entity to setup a developers team. npm组织是设置开发人员团队的实体。 I believe Microsoft setup the @types organization in npm and added the TypeScript developer team to the organization. 我相信Microsoft在npm中设置了@types组织,并将TypeScript开发团队添加到组织中。 Packages on under the @types organization are published automatically from DefinitelyTyped using the types-publisher tool as per the docs . 在@types组织下软件包自动从DefinitelyTyped使用类型的出版商工具按已发布的文档

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. 如果您的包有一个主.js文件,您还需要在package.json文件中指明主声明文件。 Set the types property to point to your bundled declaration file. types属性设置为指向捆绑的声明文件。 For example: 例如:

{
    "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/types to 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. 但是当我在npm search @types/openlayers ,我没有得到任何结果。 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不会跨组织搜索。

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 . @types npm组织用于获取npm类型定义。 Using these type definitions is a feature is coming in TypeScript 2.0. 使用这些类型定义是TypeScript 2.0中的一项功能。

This will replace the current projects/tools such as typings and tsd , though these will continue to be supported for some time. 这将取代目前的工程/工具,如分型TSD ,虽然这些会持续一段时间的支持。

This is going to be a feature that is rolled out in Typescript 2.0. 这将是一个在Typescript 2.0中推出的功能。 This provides type support for UMD Modules/Libraries and their respective definitions. 这为UMD模块/库及其各自的定义提供了类型支持。

See ( Built-in support for UMD module definitions ) to get a better understanding of the issues currently with ambient typings. 请参阅( 内置支持UMD模块定义 )以更好地了解当前环境类型的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM