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

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

TypeScript typings in npm @types org packages

typescripttypescript-typings

提问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/openlayerspackage, but typings search npm:openlayersreturns 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 typesto your packages:

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

In your package.json

在你的 package.json 中

If your package has a main .jsfile, you will need to indicate the main declaration file in your package.jsonfile as well. Set the typesproperty 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.tsand 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 searchdoes 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 @typesnpm organization is for obtaining type definitions with npm. Using these type definitions is a feature is coming in TypeScript 2.0.

@typesNPM组织是获得与类型定义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.

这将取代目前的工程/工具,如分型TSD,虽然这些会持续一段时间的支持。

回答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 模块定义的内置支持)以更好地了解当前环境类型的问题。