简体   繁体   English

npm软件包的可安装打字稿类型定义

[英]Installable typescript type definitions for an npm package

Typings are one the best features of Typescript and I find it very useful, to adopt it all over the javascript project, however the usage and best practices are not documented very efficient that causes a lot of confusion for the developers. 键入是Typescript的最佳功能之一,我发现在整个javascript项目中都采用它非常有用,但是记录的用法和最佳实践并未非常有效,这给开发人员造成了很多困惑。

For example my npm package named my-awesome-pkg and for this package we are interested to have @typings, that users are able to install it using npm npm install @types/my-awesome-pkg 例如,我的npm软件包名为my-awesome-pkg ,对于此软件包,我们有兴趣使用@typings,用户可以使用npm进行安装npm install @types/my-awesome-pkg

I know how to use include or files in tsconfig.json to include and use the @typings in your project : 我知道如何在tsconfig.json使用includefilestsconfig.json中包含和使用@typings:

"include": [ "< path to your typing file>" ]

But that is not my what I am looking for, I am interested to know, how to create installable @typings for my project, so users don't get frustrated looking for the typings of my npm package, as assume there is an standard practice for doing it. 但这不是我要寻找的东西,我有兴趣知道如何为我的项目创建可安装的@typings,因此,假设存在标准做法,那么用户就不会因寻找我的npm软件包的类型而感到沮丧。为做到这一点。

Thanks, 谢谢,

So in case that your project is written in TypeScript all you have to do is to set the following flags in your project's tsconfig.json : 因此,如果您的项目是用TypeScript编写的,那么您要做的就是在项目的tsconfig.json设置以下标志:

"declaration": true,
"declarationDir": './anyFolderYouLike'

This will generate types for your TypeScript project when tsc is run. 运行tsc时,这将为您的TypeScript项目生成类型。 For further information on how to configure the tsc see here . 有关如何配置tsc更多信息,请参见此处

Many people host their types on DefinitelyTyped . 许多人将其类型托管在DefinitelyTyped上 That way one should be able to install the types the way you wish to. 这样一来,便应该能够按照您希望的方式安装类型。

I am seeing more and more folks delivering their types with their own package though. 我看到越来越多的人通过自己的软件包交付自己的类型。 This way there is no need to install them separately. 这样,无需单独安装它们。 If you want to do it that way you should specify the types location in the package.json , ie: 如果要这样做,则应在package.json指定类型位置,即:

"types": "./lib/index.d.ts"

For further overall information on publishing please refer to the TypeScript publishing documentation . 有关发布的更多总体信息,请参考TypeScript发布文档

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

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