npm @types org 包中的 TypeScript 类型 [英] TypeScript typings in npm @types org packages

查看:38
本文介绍了npm @types org 包中的 TypeScript 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到有一个 npm 组织 @types,其中包含类型包,但不能'没有找到任何关于它的文档.这些是如何使用的?

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?

它是否意味着与 typings 工具一起使用?如果是这样,如何安装它们?例如,有一个 @types/openlayers 包,但是 typings search npm:openlayers 什么都不返回.

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.

它是否意味着与打字工具分开使用?例如.直接用 npm 安装?

Is it meant to be used separately from the typings tool? E.g. installed directly with npm?

推荐答案

从 TypeScript 2.0 开始,不再需要 typings.npm 组织是建立开发团队的实体.我相信微软在 npm 中设置了 @types 组织,并将 TypeScript 开发人员团队添加到该组织中.根据 文档.

As of TypeScript 2.0, typings is 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.

此外,还有另一种方法可以将 types 添加到您的包中:

In addition, to there is another way to add types to your packages:

在你的 package.json

如果您的包有一个主 .js 文件,您还需要在 package.json 文件中指明主声明文件.将 types 属性设置为指向您的捆绑声明文件.例如:

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:

{
    "name": "awesome",
    "author": "Vandelay Industries",
    "version": "1.0.0",
    "main": "./lib/main.js",
    "types": "./lib/main.d.ts"
}

请注意,"typings" 字段与 "types" 同义,也可以使用.

Note that the "typings" field is synonymous with "types", and could be used as well.

另请注意,如果您的主声明文件名为 index.d.ts 并且位于包的根目录(在 index.js 旁边),则您不会需要标记 "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.

关于搜索类型

在大多数情况下,类型声明包应该始终与 npm 上的包名具有相同的名称,但要以@types/为前缀,但如果您需要,可以查看 https://aka.ms/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.

来自 - http://www.typescriptlang.org/docs/手册/声明文件/consumption.html

但是当我进行 npm search @types/openlayers 时,我没有得到任何结果.但是从 Web 界面进行搜索确实返回了结果.所以我猜 npm search 不会跨组织搜索.

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 @types org 包中的 TypeScript 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆